]> Git Repo - qemu.git/blame - hw/i386/amd_iommu.c
x86_iommu/amd: make the address space naming consistent with intel-iommu
[qemu.git] / hw / i386 / amd_iommu.c
CommitLineData
d29a09ca
DK
1/*
2 * QEMU emulation of AMD IOMMU (AMD-Vi)
3 *
4 * Copyright (C) 2011 Eduard - Gabriel Munteanu
5 * Copyright (C) 2015 David Kiarie, <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Cache implementation inspired by hw/i386/intel_iommu.c
21 */
22#include "qemu/osdep.h"
433545d5
PMD
23#include "hw/i386/pc.h"
24#include "hw/pci/msi.h"
25#include "hw/pci/pci_bus.h"
26#include "amd_iommu.h"
ef0e8fc7 27#include "qapi/error.h"
a3276f78 28#include "qemu/error-report.h"
d29a09ca
DK
29#include "trace.h"
30
31/* used AMD-Vi MMIO registers */
32const char *amdvi_mmio_low[] = {
33 "AMDVI_MMIO_DEVTAB_BASE",
34 "AMDVI_MMIO_CMDBUF_BASE",
35 "AMDVI_MMIO_EVTLOG_BASE",
36 "AMDVI_MMIO_CONTROL",
37 "AMDVI_MMIO_EXCL_BASE",
38 "AMDVI_MMIO_EXCL_LIMIT",
39 "AMDVI_MMIO_EXT_FEATURES",
40 "AMDVI_MMIO_PPR_BASE",
41 "UNHANDLED"
42};
43const char *amdvi_mmio_high[] = {
44 "AMDVI_MMIO_COMMAND_HEAD",
45 "AMDVI_MMIO_COMMAND_TAIL",
46 "AMDVI_MMIO_EVTLOG_HEAD",
47 "AMDVI_MMIO_EVTLOG_TAIL",
48 "AMDVI_MMIO_STATUS",
49 "AMDVI_MMIO_PPR_HEAD",
50 "AMDVI_MMIO_PPR_TAIL",
51 "UNHANDLED"
52};
53
54struct AMDVIAddressSpace {
55 uint8_t bus_num; /* bus number */
56 uint8_t devfn; /* device function */
57 AMDVIState *iommu_state; /* AMDVI - one per machine */
53244386 58 MemoryRegion root; /* AMDVI Root memory map region */
3df9d748 59 IOMMUMemoryRegion iommu; /* Device's address translation region */
d29a09ca
DK
60 MemoryRegion iommu_ir; /* Device's interrupt remapping region */
61 AddressSpace as; /* device's corresponding address space */
62};
63
64/* AMDVI cache entry */
65typedef struct AMDVIIOTLBEntry {
66 uint16_t domid; /* assigned domain id */
67 uint16_t devid; /* device owning entry */
68 uint64_t perms; /* access permissions */
69 uint64_t translated_addr; /* translated address */
70 uint64_t page_mask; /* physical page size */
71} AMDVIIOTLBEntry;
72
73/* configure MMIO registers at startup/reset */
74static void amdvi_set_quad(AMDVIState *s, hwaddr addr, uint64_t val,
75 uint64_t romask, uint64_t w1cmask)
76{
77 stq_le_p(&s->mmior[addr], val);
78 stq_le_p(&s->romask[addr], romask);
79 stq_le_p(&s->w1cmask[addr], w1cmask);
80}
81
82static uint16_t amdvi_readw(AMDVIState *s, hwaddr addr)
83{
84 return lduw_le_p(&s->mmior[addr]);
85}
86
87static uint32_t amdvi_readl(AMDVIState *s, hwaddr addr)
88{
89 return ldl_le_p(&s->mmior[addr]);
90}
91
92static uint64_t amdvi_readq(AMDVIState *s, hwaddr addr)
93{
94 return ldq_le_p(&s->mmior[addr]);
95}
96
97/* internal write */
98static void amdvi_writeq_raw(AMDVIState *s, uint64_t val, hwaddr addr)
99{
100 stq_le_p(&s->mmior[addr], val);
101}
102
103/* external write */
104static void amdvi_writew(AMDVIState *s, hwaddr addr, uint16_t val)
105{
106 uint16_t romask = lduw_le_p(&s->romask[addr]);
107 uint16_t w1cmask = lduw_le_p(&s->w1cmask[addr]);
108 uint16_t oldval = lduw_le_p(&s->mmior[addr]);
109 stw_le_p(&s->mmior[addr],
110 ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
111}
112
113static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
114{
115 uint32_t romask = ldl_le_p(&s->romask[addr]);
116 uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
117 uint32_t oldval = ldl_le_p(&s->mmior[addr]);
118 stl_le_p(&s->mmior[addr],
119 ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
120}
121
122static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
123{
124 uint64_t romask = ldq_le_p(&s->romask[addr]);
125 uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
126 uint32_t oldval = ldq_le_p(&s->mmior[addr]);
127 stq_le_p(&s->mmior[addr],
128 ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
129}
130
131/* OR a 64-bit register with a 64-bit value */
132static bool amdvi_test_mask(AMDVIState *s, hwaddr addr, uint64_t val)
133{
134 return amdvi_readq(s, addr) | val;
135}
136
137/* OR a 64-bit register with a 64-bit value storing result in the register */
138static void amdvi_assign_orq(AMDVIState *s, hwaddr addr, uint64_t val)
139{
140 amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) | val);
141}
142
143/* AND a 64-bit register with a 64-bit value storing result in the register */
144static void amdvi_assign_andq(AMDVIState *s, hwaddr addr, uint64_t val)
145{
146 amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) & val);
147}
148
149static void amdvi_generate_msi_interrupt(AMDVIState *s)
150{
1d5b128c
DK
151 MSIMessage msg = {};
152 MemTxAttrs attrs = {
153 .requester_id = pci_requester_id(&s->pci.dev)
154 };
d29a09ca
DK
155
156 if (msi_enabled(&s->pci.dev)) {
157 msg = msi_get_message(&s->pci.dev, 0);
158 address_space_stl_le(&address_space_memory, msg.address, msg.data,
159 attrs, NULL);
160 }
161}
162
163static void amdvi_log_event(AMDVIState *s, uint64_t *evt)
164{
165 /* event logging not enabled */
166 if (!s->evtlog_enabled || amdvi_test_mask(s, AMDVI_MMIO_STATUS,
167 AMDVI_MMIO_STATUS_EVT_OVF)) {
168 return;
169 }
170
171 /* event log buffer full */
172 if (s->evtlog_tail >= s->evtlog_len) {
173 amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF);
174 /* generate interrupt */
175 amdvi_generate_msi_interrupt(s);
176 return;
177 }
178
179 if (dma_memory_write(&address_space_memory, s->evtlog + s->evtlog_tail,
180 &evt, AMDVI_EVENT_LEN)) {
181 trace_amdvi_evntlog_fail(s->evtlog, s->evtlog_tail);
182 }
183
184 s->evtlog_tail += AMDVI_EVENT_LEN;
185 amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT);
186 amdvi_generate_msi_interrupt(s);
187}
188
189static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
190 int length)
191{
192 int index = start / 64, bitpos = start % 64;
1d5b128c 193 uint64_t mask = MAKE_64BIT_MASK(start, length);
d29a09ca
DK
194 buffer[index] &= ~mask;
195 buffer[index] |= (value << bitpos) & mask;
196}
197/*
198 * AMDVi event structure
199 * 0:15 -> DeviceID
200 * 55:63 -> event type + miscellaneous info
201 * 63:127 -> related address
202 */
203static void amdvi_encode_event(uint64_t *evt, uint16_t devid, uint64_t addr,
204 uint16_t info)
205{
206 amdvi_setevent_bits(evt, devid, 0, 16);
207 amdvi_setevent_bits(evt, info, 55, 8);
208 amdvi_setevent_bits(evt, addr, 63, 64);
209}
210/* log an error encountered during a page walk
211 *
212 * @addr: virtual address in translation request
213 */
214static void amdvi_page_fault(AMDVIState *s, uint16_t devid,
215 hwaddr addr, uint16_t info)
216{
217 uint64_t evt[4];
218
219 info |= AMDVI_EVENT_IOPF_I | AMDVI_EVENT_IOPF;
220 amdvi_encode_event(evt, devid, addr, info);
221 amdvi_log_event(s, evt);
222 pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
223 PCI_STATUS_SIG_TARGET_ABORT);
224}
225/*
226 * log a master abort accessing device table
227 * @devtab : address of device table entry
228 * @info : error flags
229 */
230static void amdvi_log_devtab_error(AMDVIState *s, uint16_t devid,
231 hwaddr devtab, uint16_t info)
232{
233 uint64_t evt[4];
234
235 info |= AMDVI_EVENT_DEV_TAB_HW_ERROR;
236
237 amdvi_encode_event(evt, devid, devtab, info);
238 amdvi_log_event(s, evt);
239 pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
240 PCI_STATUS_SIG_TARGET_ABORT);
241}
242/* log an event trying to access command buffer
243 * @addr : address that couldn't be accessed
244 */
245static void amdvi_log_command_error(AMDVIState *s, hwaddr addr)
246{
247 uint64_t evt[4], info = AMDVI_EVENT_COMMAND_HW_ERROR;
248
249 amdvi_encode_event(evt, 0, addr, info);
250 amdvi_log_event(s, evt);
251 pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
252 PCI_STATUS_SIG_TARGET_ABORT);
253}
254/* log an illegal comand event
255 * @addr : address of illegal command
256 */
257static void amdvi_log_illegalcom_error(AMDVIState *s, uint16_t info,
258 hwaddr addr)
259{
260 uint64_t evt[4];
261
262 info |= AMDVI_EVENT_ILLEGAL_COMMAND_ERROR;
263 amdvi_encode_event(evt, 0, addr, info);
264 amdvi_log_event(s, evt);
265}
266/* log an error accessing device table
267 *
268 * @devid : device owning the table entry
269 * @devtab : address of device table entry
270 * @info : error flags
271 */
272static void amdvi_log_illegaldevtab_error(AMDVIState *s, uint16_t devid,
273 hwaddr addr, uint16_t info)
274{
275 uint64_t evt[4];
276
277 info |= AMDVI_EVENT_ILLEGAL_DEVTAB_ENTRY;
278 amdvi_encode_event(evt, devid, addr, info);
279 amdvi_log_event(s, evt);
280}
281/* log an error accessing a PTE entry
282 * @addr : address that couldn't be accessed
283 */
284static void amdvi_log_pagetab_error(AMDVIState *s, uint16_t devid,
285 hwaddr addr, uint16_t info)
286{
287 uint64_t evt[4];
288
289 info |= AMDVI_EVENT_PAGE_TAB_HW_ERROR;
290 amdvi_encode_event(evt, devid, addr, info);
291 amdvi_log_event(s, evt);
292 pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
293 PCI_STATUS_SIG_TARGET_ABORT);
294}
295
296static gboolean amdvi_uint64_equal(gconstpointer v1, gconstpointer v2)
297{
298 return *((const uint64_t *)v1) == *((const uint64_t *)v2);
299}
300
301static guint amdvi_uint64_hash(gconstpointer v)
302{
303 return (guint)*(const uint64_t *)v;
304}
305
306static AMDVIIOTLBEntry *amdvi_iotlb_lookup(AMDVIState *s, hwaddr addr,
307 uint64_t devid)
308{
309 uint64_t key = (addr >> AMDVI_PAGE_SHIFT_4K) |
310 ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
311 return g_hash_table_lookup(s->iotlb, &key);
312}
313
314static void amdvi_iotlb_reset(AMDVIState *s)
315{
316 assert(s->iotlb);
317 trace_amdvi_iotlb_reset();
318 g_hash_table_remove_all(s->iotlb);
319}
320
321static gboolean amdvi_iotlb_remove_by_devid(gpointer key, gpointer value,
322 gpointer user_data)
323{
324 AMDVIIOTLBEntry *entry = (AMDVIIOTLBEntry *)value;
325 uint16_t devid = *(uint16_t *)user_data;
326 return entry->devid == devid;
327}
328
329static void amdvi_iotlb_remove_page(AMDVIState *s, hwaddr addr,
330 uint64_t devid)
331{
332 uint64_t key = (addr >> AMDVI_PAGE_SHIFT_4K) |
333 ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
334 g_hash_table_remove(s->iotlb, &key);
335}
336
337static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
338 uint64_t gpa, IOMMUTLBEntry to_cache,
339 uint16_t domid)
340{
1d5b128c
DK
341 AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
342 uint64_t *key = g_new(uint64_t, 1);
d29a09ca
DK
343 uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
344
345 /* don't cache erroneous translations */
346 if (to_cache.perm != IOMMU_NONE) {
347 trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),
348 PCI_FUNC(devid), gpa, to_cache.translated_addr);
349
350 if (g_hash_table_size(s->iotlb) >= AMDVI_IOTLB_MAX_SIZE) {
351 amdvi_iotlb_reset(s);
352 }
353
354 entry->domid = domid;
355 entry->perms = to_cache.perm;
356 entry->translated_addr = to_cache.translated_addr;
357 entry->page_mask = to_cache.addr_mask;
358 *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
359 g_hash_table_replace(s->iotlb, key, entry);
360 }
361}
362
363static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
364{
365 /* pad the last 3 bits */
366 hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;
367 uint64_t data = cpu_to_le64(cmd[1]);
368
369 if (extract64(cmd[0], 51, 8)) {
370 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
371 s->cmdbuf + s->cmdbuf_head);
372 }
373 if (extract64(cmd[0], 0, 1)) {
374 if (dma_memory_write(&address_space_memory, addr, &data,
375 AMDVI_COMPLETION_DATA_SIZE)) {
376 trace_amdvi_completion_wait_fail(addr);
377 }
378 }
379 /* set completion interrupt */
380 if (extract64(cmd[0], 1, 1)) {
381 amdvi_test_mask(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT);
382 /* generate interrupt */
383 amdvi_generate_msi_interrupt(s);
384 }
385 trace_amdvi_completion_wait(addr, data);
386}
387
388/* log error without aborting since linux seems to be using reserved bits */
389static void amdvi_inval_devtab_entry(AMDVIState *s, uint64_t *cmd)
390{
391 uint16_t devid = cpu_to_le16((uint16_t)extract64(cmd[0], 0, 16));
392
393 /* This command should invalidate internal caches of which there isn't */
394 if (extract64(cmd[0], 15, 16) || cmd[1]) {
395 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
396 s->cmdbuf + s->cmdbuf_head);
397 }
398 trace_amdvi_devtab_inval(PCI_BUS_NUM(devid), PCI_SLOT(devid),
399 PCI_FUNC(devid));
400}
401
402static void amdvi_complete_ppr(AMDVIState *s, uint64_t *cmd)
403{
404 if (extract64(cmd[0], 15, 16) || extract64(cmd[0], 19, 8) ||
405 extract64(cmd[1], 0, 2) || extract64(cmd[1], 3, 29)
406 || extract64(cmd[1], 47, 16)) {
407 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
408 s->cmdbuf + s->cmdbuf_head);
409 }
410 trace_amdvi_ppr_exec();
411}
412
413static void amdvi_inval_all(AMDVIState *s, uint64_t *cmd)
414{
415 if (extract64(cmd[0], 0, 60) || cmd[1]) {
416 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
417 s->cmdbuf + s->cmdbuf_head);
418 }
419
420 amdvi_iotlb_reset(s);
421 trace_amdvi_all_inval();
422}
423
424static gboolean amdvi_iotlb_remove_by_domid(gpointer key, gpointer value,
425 gpointer user_data)
426{
427 AMDVIIOTLBEntry *entry = (AMDVIIOTLBEntry *)value;
428 uint16_t domid = *(uint16_t *)user_data;
429 return entry->domid == domid;
430}
431
432/* we don't have devid - we can't remove pages by address */
433static void amdvi_inval_pages(AMDVIState *s, uint64_t *cmd)
434{
435 uint16_t domid = cpu_to_le16((uint16_t)extract64(cmd[0], 32, 16));
436
437 if (extract64(cmd[0], 20, 12) || extract64(cmd[0], 16, 12) ||
438 extract64(cmd[0], 3, 10)) {
439 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
440 s->cmdbuf + s->cmdbuf_head);
441 }
442
443 g_hash_table_foreach_remove(s->iotlb, amdvi_iotlb_remove_by_domid,
444 &domid);
445 trace_amdvi_pages_inval(domid);
446}
447
448static void amdvi_prefetch_pages(AMDVIState *s, uint64_t *cmd)
449{
450 if (extract64(cmd[0], 16, 8) || extract64(cmd[0], 20, 8) ||
451 extract64(cmd[1], 1, 1) || extract64(cmd[1], 3, 1) ||
452 extract64(cmd[1], 5, 7)) {
453 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
454 s->cmdbuf + s->cmdbuf_head);
455 }
456
457 trace_amdvi_prefetch_pages();
458}
459
460static void amdvi_inval_inttable(AMDVIState *s, uint64_t *cmd)
461{
462 if (extract64(cmd[0], 16, 16) || cmd[1]) {
463 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
464 s->cmdbuf + s->cmdbuf_head);
465 return;
466 }
467
468 trace_amdvi_intr_inval();
469}
470
471/* FIXME: Try to work with the specified size instead of all the pages
472 * when the S bit is on
473 */
474static void iommu_inval_iotlb(AMDVIState *s, uint64_t *cmd)
475{
476
477 uint16_t devid = extract64(cmd[0], 0, 16);
478 if (extract64(cmd[1], 1, 1) || extract64(cmd[1], 3, 9)) {
479 amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
480 s->cmdbuf + s->cmdbuf_head);
481 return;
482 }
483
484 if (extract64(cmd[1], 0, 1)) {
485 g_hash_table_foreach_remove(s->iotlb, amdvi_iotlb_remove_by_devid,
486 &devid);
487 } else {
488 amdvi_iotlb_remove_page(s, cpu_to_le64(extract64(cmd[1], 12, 52)) << 12,
489 cpu_to_le16(extract64(cmd[1], 0, 16)));
490 }
491 trace_amdvi_iotlb_inval();
492}
493
494/* not honouring reserved bits is regarded as an illegal command */
495static void amdvi_cmdbuf_exec(AMDVIState *s)
496{
497 uint64_t cmd[2];
498
499 if (dma_memory_read(&address_space_memory, s->cmdbuf + s->cmdbuf_head,
500 cmd, AMDVI_COMMAND_SIZE)) {
501 trace_amdvi_command_read_fail(s->cmdbuf, s->cmdbuf_head);
502 amdvi_log_command_error(s, s->cmdbuf + s->cmdbuf_head);
503 return;
504 }
505
506 switch (extract64(cmd[0], 60, 4)) {
507 case AMDVI_CMD_COMPLETION_WAIT:
508 amdvi_completion_wait(s, cmd);
509 break;
510 case AMDVI_CMD_INVAL_DEVTAB_ENTRY:
511 amdvi_inval_devtab_entry(s, cmd);
512 break;
513 case AMDVI_CMD_INVAL_AMDVI_PAGES:
514 amdvi_inval_pages(s, cmd);
515 break;
516 case AMDVI_CMD_INVAL_IOTLB_PAGES:
517 iommu_inval_iotlb(s, cmd);
518 break;
519 case AMDVI_CMD_INVAL_INTR_TABLE:
520 amdvi_inval_inttable(s, cmd);
521 break;
522 case AMDVI_CMD_PREFETCH_AMDVI_PAGES:
523 amdvi_prefetch_pages(s, cmd);
524 break;
525 case AMDVI_CMD_COMPLETE_PPR_REQUEST:
526 amdvi_complete_ppr(s, cmd);
527 break;
528 case AMDVI_CMD_INVAL_AMDVI_ALL:
529 amdvi_inval_all(s, cmd);
530 break;
531 default:
532 trace_amdvi_unhandled_command(extract64(cmd[1], 60, 4));
533 /* log illegal command */
534 amdvi_log_illegalcom_error(s, extract64(cmd[1], 60, 4),
535 s->cmdbuf + s->cmdbuf_head);
536 }
537}
538
539static void amdvi_cmdbuf_run(AMDVIState *s)
540{
541 if (!s->cmdbuf_enabled) {
542 trace_amdvi_command_error(amdvi_readq(s, AMDVI_MMIO_CONTROL));
543 return;
544 }
545
546 /* check if there is work to do. */
547 while (s->cmdbuf_head != s->cmdbuf_tail) {
548 trace_amdvi_command_exec(s->cmdbuf_head, s->cmdbuf_tail, s->cmdbuf);
549 amdvi_cmdbuf_exec(s);
550 s->cmdbuf_head += AMDVI_COMMAND_SIZE;
551 amdvi_writeq_raw(s, s->cmdbuf_head, AMDVI_MMIO_COMMAND_HEAD);
552
553 /* wrap head pointer */
554 if (s->cmdbuf_head >= s->cmdbuf_len * AMDVI_COMMAND_SIZE) {
555 s->cmdbuf_head = 0;
556 }
557 }
558}
559
560static void amdvi_mmio_trace(hwaddr addr, unsigned size)
561{
562 uint8_t index = (addr & ~0x2000) / 8;
563
564 if ((addr & 0x2000)) {
565 /* high table */
566 index = index >= AMDVI_MMIO_REGS_HIGH ? AMDVI_MMIO_REGS_HIGH : index;
567 trace_amdvi_mmio_read(amdvi_mmio_high[index], addr, size, addr & ~0x07);
568 } else {
569 index = index >= AMDVI_MMIO_REGS_LOW ? AMDVI_MMIO_REGS_LOW : index;
d9429b84 570 trace_amdvi_mmio_read(amdvi_mmio_low[index], addr, size, addr & ~0x07);
d29a09ca
DK
571 }
572}
573
574static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
575{
576 AMDVIState *s = opaque;
577
578 uint64_t val = -1;
579 if (addr + size > AMDVI_MMIO_SIZE) {
0d3ef788 580 trace_amdvi_mmio_read_invalid(AMDVI_MMIO_SIZE, addr, size);
d29a09ca
DK
581 return (uint64_t)-1;
582 }
583
584 if (size == 2) {
585 val = amdvi_readw(s, addr);
586 } else if (size == 4) {
587 val = amdvi_readl(s, addr);
588 } else if (size == 8) {
589 val = amdvi_readq(s, addr);
590 }
591 amdvi_mmio_trace(addr, size);
592
593 return val;
594}
595
596static void amdvi_handle_control_write(AMDVIState *s)
597{
598 unsigned long control = amdvi_readq(s, AMDVI_MMIO_CONTROL);
599 s->enabled = !!(control & AMDVI_MMIO_CONTROL_AMDVIEN);
600
601 s->ats_enabled = !!(control & AMDVI_MMIO_CONTROL_HTTUNEN);
602 s->evtlog_enabled = s->enabled && !!(control &
603 AMDVI_MMIO_CONTROL_EVENTLOGEN);
604
605 s->evtlog_intr = !!(control & AMDVI_MMIO_CONTROL_EVENTINTEN);
606 s->completion_wait_intr = !!(control & AMDVI_MMIO_CONTROL_COMWAITINTEN);
607 s->cmdbuf_enabled = s->enabled && !!(control &
608 AMDVI_MMIO_CONTROL_CMDBUFLEN);
609
610 /* update the flags depending on the control register */
611 if (s->cmdbuf_enabled) {
612 amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_CMDBUF_RUN);
613 } else {
614 amdvi_assign_andq(s, AMDVI_MMIO_STATUS, ~AMDVI_MMIO_STATUS_CMDBUF_RUN);
615 }
616 if (s->evtlog_enabled) {
617 amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_RUN);
618 } else {
619 amdvi_assign_andq(s, AMDVI_MMIO_STATUS, ~AMDVI_MMIO_STATUS_EVT_RUN);
620 }
621
622 trace_amdvi_control_status(control);
623 amdvi_cmdbuf_run(s);
624}
625
626static inline void amdvi_handle_devtab_write(AMDVIState *s)
627
628{
629 uint64_t val = amdvi_readq(s, AMDVI_MMIO_DEVICE_TABLE);
630 s->devtab = (val & AMDVI_MMIO_DEVTAB_BASE_MASK);
631
632 /* set device table length */
633 s->devtab_len = ((val & AMDVI_MMIO_DEVTAB_SIZE_MASK) + 1 *
634 (AMDVI_MMIO_DEVTAB_SIZE_UNIT /
635 AMDVI_MMIO_DEVTAB_ENTRY_SIZE));
636}
637
638static inline void amdvi_handle_cmdhead_write(AMDVIState *s)
639{
640 s->cmdbuf_head = amdvi_readq(s, AMDVI_MMIO_COMMAND_HEAD)
641 & AMDVI_MMIO_CMDBUF_HEAD_MASK;
642 amdvi_cmdbuf_run(s);
643}
644
645static inline void amdvi_handle_cmdbase_write(AMDVIState *s)
646{
647 s->cmdbuf = amdvi_readq(s, AMDVI_MMIO_COMMAND_BASE)
648 & AMDVI_MMIO_CMDBUF_BASE_MASK;
649 s->cmdbuf_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_CMDBUF_SIZE_BYTE)
650 & AMDVI_MMIO_CMDBUF_SIZE_MASK);
651 s->cmdbuf_head = s->cmdbuf_tail = 0;
652}
653
654static inline void amdvi_handle_cmdtail_write(AMDVIState *s)
655{
656 s->cmdbuf_tail = amdvi_readq(s, AMDVI_MMIO_COMMAND_TAIL)
657 & AMDVI_MMIO_CMDBUF_TAIL_MASK;
658 amdvi_cmdbuf_run(s);
659}
660
661static inline void amdvi_handle_excllim_write(AMDVIState *s)
662{
663 uint64_t val = amdvi_readq(s, AMDVI_MMIO_EXCL_LIMIT);
664 s->excl_limit = (val & AMDVI_MMIO_EXCL_LIMIT_MASK) |
665 AMDVI_MMIO_EXCL_LIMIT_LOW;
666}
667
668static inline void amdvi_handle_evtbase_write(AMDVIState *s)
669{
670 uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_BASE);
671 s->evtlog = val & AMDVI_MMIO_EVTLOG_BASE_MASK;
672 s->evtlog_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_EVTLOG_SIZE_BYTE)
673 & AMDVI_MMIO_EVTLOG_SIZE_MASK);
674}
675
676static inline void amdvi_handle_evttail_write(AMDVIState *s)
677{
678 uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_TAIL);
679 s->evtlog_tail = val & AMDVI_MMIO_EVTLOG_TAIL_MASK;
680}
681
682static inline void amdvi_handle_evthead_write(AMDVIState *s)
683{
684 uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_HEAD);
685 s->evtlog_head = val & AMDVI_MMIO_EVTLOG_HEAD_MASK;
686}
687
688static inline void amdvi_handle_pprbase_write(AMDVIState *s)
689{
690 uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_BASE);
691 s->ppr_log = val & AMDVI_MMIO_PPRLOG_BASE_MASK;
692 s->pprlog_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_PPRLOG_SIZE_BYTE)
693 & AMDVI_MMIO_PPRLOG_SIZE_MASK);
694}
695
696static inline void amdvi_handle_pprhead_write(AMDVIState *s)
697{
698 uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_HEAD);
699 s->pprlog_head = val & AMDVI_MMIO_PPRLOG_HEAD_MASK;
700}
701
702static inline void amdvi_handle_pprtail_write(AMDVIState *s)
703{
704 uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_TAIL);
705 s->pprlog_tail = val & AMDVI_MMIO_PPRLOG_TAIL_MASK;
706}
707
708/* FIXME: something might go wrong if System Software writes in chunks
709 * of one byte but linux writes in chunks of 4 bytes so currently it
710 * works correctly with linux but will definitely be busted if software
711 * reads/writes 8 bytes
712 */
713static void amdvi_mmio_reg_write(AMDVIState *s, unsigned size, uint64_t val,
714 hwaddr addr)
715{
716 if (size == 2) {
717 amdvi_writew(s, addr, val);
718 } else if (size == 4) {
719 amdvi_writel(s, addr, val);
720 } else if (size == 8) {
721 amdvi_writeq(s, addr, val);
722 }
723}
724
725static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
726 unsigned size)
727{
728 AMDVIState *s = opaque;
729 unsigned long offset = addr & 0x07;
730
731 if (addr + size > AMDVI_MMIO_SIZE) {
732 trace_amdvi_mmio_write("error: addr outside region: max ",
733 (uint64_t)AMDVI_MMIO_SIZE, size, val, offset);
734 return;
735 }
736
737 amdvi_mmio_trace(addr, size);
738 switch (addr & ~0x07) {
739 case AMDVI_MMIO_CONTROL:
740 amdvi_mmio_reg_write(s, size, val, addr);
741 amdvi_handle_control_write(s);
742 break;
743 case AMDVI_MMIO_DEVICE_TABLE:
744 amdvi_mmio_reg_write(s, size, val, addr);
745 /* set device table address
746 * This also suffers from inability to tell whether software
747 * is done writing
748 */
749 if (offset || (size == 8)) {
750 amdvi_handle_devtab_write(s);
751 }
752 break;
753 case AMDVI_MMIO_COMMAND_HEAD:
754 amdvi_mmio_reg_write(s, size, val, addr);
755 amdvi_handle_cmdhead_write(s);
756 break;
757 case AMDVI_MMIO_COMMAND_BASE:
758 amdvi_mmio_reg_write(s, size, val, addr);
759 /* FIXME - make sure System Software has finished writing incase
760 * it writes in chucks less than 8 bytes in a robust way.As for
761 * now, this hacks works for the linux driver
762 */
763 if (offset || (size == 8)) {
764 amdvi_handle_cmdbase_write(s);
765 }
766 break;
767 case AMDVI_MMIO_COMMAND_TAIL:
768 amdvi_mmio_reg_write(s, size, val, addr);
769 amdvi_handle_cmdtail_write(s);
770 break;
771 case AMDVI_MMIO_EVENT_BASE:
772 amdvi_mmio_reg_write(s, size, val, addr);
773 amdvi_handle_evtbase_write(s);
774 break;
775 case AMDVI_MMIO_EVENT_HEAD:
776 amdvi_mmio_reg_write(s, size, val, addr);
777 amdvi_handle_evthead_write(s);
778 break;
779 case AMDVI_MMIO_EVENT_TAIL:
780 amdvi_mmio_reg_write(s, size, val, addr);
781 amdvi_handle_evttail_write(s);
782 break;
783 case AMDVI_MMIO_EXCL_LIMIT:
784 amdvi_mmio_reg_write(s, size, val, addr);
785 amdvi_handle_excllim_write(s);
786 break;
787 /* PPR log base - unused for now */
788 case AMDVI_MMIO_PPR_BASE:
789 amdvi_mmio_reg_write(s, size, val, addr);
790 amdvi_handle_pprbase_write(s);
791 break;
792 /* PPR log head - also unused for now */
793 case AMDVI_MMIO_PPR_HEAD:
794 amdvi_mmio_reg_write(s, size, val, addr);
795 amdvi_handle_pprhead_write(s);
796 break;
797 /* PPR log tail - unused for now */
798 case AMDVI_MMIO_PPR_TAIL:
799 amdvi_mmio_reg_write(s, size, val, addr);
800 amdvi_handle_pprtail_write(s);
801 break;
802 }
803}
804
805static inline uint64_t amdvi_get_perms(uint64_t entry)
806{
807 return (entry & (AMDVI_DEV_PERM_READ | AMDVI_DEV_PERM_WRITE)) >>
808 AMDVI_DEV_PERM_SHIFT;
809}
810
470506b5 811/* validate that reserved bits are honoured */
d29a09ca
DK
812static bool amdvi_validate_dte(AMDVIState *s, uint16_t devid,
813 uint64_t *dte)
814{
815 if ((dte[0] & AMDVI_DTE_LOWER_QUAD_RESERVED)
816 || (dte[1] & AMDVI_DTE_MIDDLE_QUAD_RESERVED)
817 || (dte[2] & AMDVI_DTE_UPPER_QUAD_RESERVED) || dte[3]) {
818 amdvi_log_illegaldevtab_error(s, devid,
819 s->devtab +
820 devid * AMDVI_DEVTAB_ENTRY_SIZE, 0);
821 return false;
822 }
823
470506b5 824 return true;
d29a09ca
DK
825}
826
827/* get a device table entry given the devid */
828static bool amdvi_get_dte(AMDVIState *s, int devid, uint64_t *entry)
829{
830 uint32_t offset = devid * AMDVI_DEVTAB_ENTRY_SIZE;
831
832 if (dma_memory_read(&address_space_memory, s->devtab + offset, entry,
833 AMDVI_DEVTAB_ENTRY_SIZE)) {
834 trace_amdvi_dte_get_fail(s->devtab, offset);
835 /* log error accessing dte */
836 amdvi_log_devtab_error(s, devid, s->devtab + offset, 0);
837 return false;
838 }
839
840 *entry = le64_to_cpu(*entry);
841 if (!amdvi_validate_dte(s, devid, entry)) {
842 trace_amdvi_invalid_dte(entry[0]);
843 return false;
844 }
845
846 return true;
847}
848
849/* get pte translation mode */
850static inline uint8_t get_pte_translation_mode(uint64_t pte)
851{
852 return (pte >> AMDVI_DEV_MODE_RSHIFT) & AMDVI_DEV_MODE_MASK;
853}
854
855static inline uint64_t pte_override_page_mask(uint64_t pte)
856{
857 uint8_t page_mask = 12;
858 uint64_t addr = (pte & AMDVI_DEV_PT_ROOT_MASK) ^ AMDVI_DEV_PT_ROOT_MASK;
859 /* find the first zero bit */
860 while (addr & 1) {
861 page_mask++;
862 addr = addr >> 1;
863 }
864
865 return ~((1ULL << page_mask) - 1);
866}
867
868static inline uint64_t pte_get_page_mask(uint64_t oldlevel)
869{
870 return ~((1UL << ((oldlevel * 9) + 3)) - 1);
871}
872
873static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,
874 uint16_t devid)
875{
876 uint64_t pte;
877
878 if (dma_memory_read(&address_space_memory, pte_addr, &pte, sizeof(pte))) {
879 trace_amdvi_get_pte_hwerror(pte_addr);
880 amdvi_log_pagetab_error(s, devid, pte_addr, 0);
881 pte = 0;
882 return pte;
883 }
884
885 pte = le64_to_cpu(pte);
886 return pte;
887}
888
889static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
890 IOMMUTLBEntry *ret, unsigned perms,
891 hwaddr addr)
892{
893 unsigned level, present, pte_perms, oldlevel;
894 uint64_t pte = dte[0], pte_addr, page_mask;
895
896 /* make sure the DTE has TV = 1 */
897 if (pte & AMDVI_DEV_TRANSLATION_VALID) {
898 level = get_pte_translation_mode(pte);
899 if (level >= 7) {
900 trace_amdvi_mode_invalid(level, addr);
901 return;
902 }
903 if (level == 0) {
904 goto no_remap;
905 }
906
907 /* we are at the leaf page table or page table encodes a huge page */
908 while (level > 0) {
909 pte_perms = amdvi_get_perms(pte);
910 present = pte & 1;
911 if (!present || perms != (perms & pte_perms)) {
912 amdvi_page_fault(as->iommu_state, as->devfn, addr, perms);
913 trace_amdvi_page_fault(addr);
914 return;
915 }
916
917 /* go to the next lower level */
918 pte_addr = pte & AMDVI_DEV_PT_ROOT_MASK;
919 /* add offset and load pte */
920 pte_addr += ((addr >> (3 + 9 * level)) & 0x1FF) << 3;
921 pte = amdvi_get_pte_entry(as->iommu_state, pte_addr, as->devfn);
922 if (!pte) {
923 return;
924 }
925 oldlevel = level;
926 level = get_pte_translation_mode(pte);
927 if (level == 0x7) {
928 break;
929 }
930 }
931
932 if (level == 0x7) {
933 page_mask = pte_override_page_mask(pte);
934 } else {
935 page_mask = pte_get_page_mask(oldlevel);
936 }
937
938 /* get access permissions from pte */
939 ret->iova = addr & page_mask;
940 ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) & page_mask;
941 ret->addr_mask = ~page_mask;
942 ret->perm = amdvi_get_perms(pte);
943 return;
944 }
945no_remap:
946 ret->iova = addr & AMDVI_PAGE_MASK_4K;
947 ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
948 ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
949 ret->perm = amdvi_get_perms(pte);
950}
951
952static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
953 bool is_write, IOMMUTLBEntry *ret)
954{
955 AMDVIState *s = as->iommu_state;
956 uint16_t devid = PCI_BUILD_BDF(as->bus_num, as->devfn);
957 AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, devid);
958 uint64_t entry[4];
959
960 if (iotlb_entry) {
961 trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),
962 PCI_FUNC(devid), addr, iotlb_entry->translated_addr);
963 ret->iova = addr & ~iotlb_entry->page_mask;
964 ret->translated_addr = iotlb_entry->translated_addr;
965 ret->addr_mask = iotlb_entry->page_mask;
966 ret->perm = iotlb_entry->perms;
967 return;
968 }
969
d29a09ca 970 if (!amdvi_get_dte(s, devid, entry)) {
470506b5
BS
971 return;
972 }
973
974 /* devices with V = 0 are not translated */
975 if (!(entry[0] & AMDVI_DEV_VALID)) {
d29a09ca
DK
976 goto out;
977 }
978
979 amdvi_page_walk(as, entry, ret,
980 is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
981
982 amdvi_update_iotlb(s, devid, addr, *ret,
983 entry[1] & AMDVI_DEV_DOMID_ID_MASK);
984 return;
985
986out:
987 ret->iova = addr & AMDVI_PAGE_MASK_4K;
988 ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
989 ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
990 ret->perm = IOMMU_RW;
991}
992
993static inline bool amdvi_is_interrupt_addr(hwaddr addr)
994{
995 return addr >= AMDVI_INT_ADDR_FIRST && addr <= AMDVI_INT_ADDR_LAST;
996}
997
3df9d748 998static IOMMUTLBEntry amdvi_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
2c91bcf2 999 IOMMUAccessFlags flag, int iommu_idx)
d29a09ca
DK
1000{
1001 AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
1002 AMDVIState *s = as->iommu_state;
1003 IOMMUTLBEntry ret = {
1004 .target_as = &address_space_memory,
1005 .iova = addr,
1006 .translated_addr = 0,
1007 .addr_mask = ~(hwaddr)0,
1008 .perm = IOMMU_NONE
1009 };
1010
1011 if (!s->enabled) {
1012 /* AMDVI disabled - corresponds to iommu=off not
1013 * failure to provide any parameter
1014 */
1015 ret.iova = addr & AMDVI_PAGE_MASK_4K;
1016 ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
1017 ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
1018 ret.perm = IOMMU_RW;
1019 return ret;
1020 } else if (amdvi_is_interrupt_addr(addr)) {
1021 ret.iova = addr & AMDVI_PAGE_MASK_4K;
1022 ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
1023 ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
1024 ret.perm = IOMMU_WO;
1025 return ret;
1026 }
1027
bf55b7af 1028 amdvi_do_translate(as, addr, flag & IOMMU_WO, &ret);
d29a09ca
DK
1029 trace_amdvi_translation_result(as->bus_num, PCI_SLOT(as->devfn),
1030 PCI_FUNC(as->devfn), addr, ret.translated_addr);
1031 return ret;
1032}
1033
1034static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
1035{
53244386 1036 char name[128];
d29a09ca 1037 AMDVIState *s = opaque;
53244386 1038 AMDVIAddressSpace **iommu_as, *amdvi_dev_as;
d29a09ca
DK
1039 int bus_num = pci_bus_num(bus);
1040
1041 iommu_as = s->address_spaces[bus_num];
1042
1043 /* allocate memory during the first run */
1044 if (!iommu_as) {
1045 iommu_as = g_malloc0(sizeof(AMDVIAddressSpace *) * PCI_DEVFN_MAX);
1046 s->address_spaces[bus_num] = iommu_as;
1047 }
1048
1049 /* set up AMD-Vi region */
1050 if (!iommu_as[devfn]) {
53244386
BS
1051 snprintf(name, sizeof(name), "amd_iommu_devfn_%d", devfn);
1052
d29a09ca
DK
1053 iommu_as[devfn] = g_malloc0(sizeof(AMDVIAddressSpace));
1054 iommu_as[devfn]->bus_num = (uint8_t)bus_num;
1055 iommu_as[devfn]->devfn = (uint8_t)devfn;
1056 iommu_as[devfn]->iommu_state = s;
1057
53244386
BS
1058 amdvi_dev_as = iommu_as[devfn];
1059
1060 /*
1061 * Memory region relationships looks like (Address range shows
1062 * only lower 32 bits to make it short in length...):
1063 *
1064 * |-----------------+-------------------+----------|
1065 * | Name | Address range | Priority |
1066 * |-----------------+-------------------+----------+
1067 * | amdvi_root | 00000000-ffffffff | 0 |
1068 * | amdvi_iommu | 00000000-ffffffff | 1 |
1069 * |-----------------+-------------------+----------|
1070 */
1071 memory_region_init_iommu(&amdvi_dev_as->iommu,
1072 sizeof(amdvi_dev_as->iommu),
1221a474
AK
1073 TYPE_AMD_IOMMU_MEMORY_REGION,
1074 OBJECT(s),
53244386
BS
1075 "amd_iommu", UINT64_MAX);
1076 memory_region_init(&amdvi_dev_as->root, OBJECT(s),
1077 "amdvi_root", UINT64_MAX);
1078 address_space_init(&amdvi_dev_as->as, &amdvi_dev_as->root, name);
1079 memory_region_add_subregion_overlap(&amdvi_dev_as->root, 0,
1080 MEMORY_REGION(&amdvi_dev_as->iommu),
1081 1);
d29a09ca
DK
1082 }
1083 return &iommu_as[devfn]->as;
1084}
1085
1086static const MemoryRegionOps mmio_mem_ops = {
1087 .read = amdvi_mmio_read,
1088 .write = amdvi_mmio_write,
1089 .endianness = DEVICE_LITTLE_ENDIAN,
1090 .impl = {
1091 .min_access_size = 1,
1092 .max_access_size = 8,
1093 .unaligned = false,
1094 },
1095 .valid = {
1096 .min_access_size = 1,
1097 .max_access_size = 8,
1098 }
1099};
1100
3df9d748 1101static void amdvi_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
5bf3d319
PX
1102 IOMMUNotifierFlag old,
1103 IOMMUNotifierFlag new)
d29a09ca
DK
1104{
1105 AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
1106
a3276f78
PX
1107 if (new & IOMMU_NOTIFIER_MAP) {
1108 error_report("device %02x.%02x.%x requires iommu notifier which is not "
1109 "currently supported", as->bus_num, PCI_SLOT(as->devfn),
1110 PCI_FUNC(as->devfn));
1111 exit(1);
1112 }
d29a09ca
DK
1113}
1114
1115static void amdvi_init(AMDVIState *s)
1116{
1117 amdvi_iotlb_reset(s);
1118
d29a09ca
DK
1119 s->devtab_len = 0;
1120 s->cmdbuf_len = 0;
1121 s->cmdbuf_head = 0;
1122 s->cmdbuf_tail = 0;
1123 s->evtlog_head = 0;
1124 s->evtlog_tail = 0;
1125 s->excl_enabled = false;
1126 s->excl_allow = false;
1127 s->mmio_enabled = false;
1128 s->enabled = false;
1129 s->ats_enabled = false;
1130 s->cmdbuf_enabled = false;
1131
1132 /* reset MMIO */
1133 memset(s->mmior, 0, AMDVI_MMIO_SIZE);
1134 amdvi_set_quad(s, AMDVI_MMIO_EXT_FEATURES, AMDVI_EXT_FEATURES,
1135 0xffffffffffffffef, 0);
1136 amdvi_set_quad(s, AMDVI_MMIO_STATUS, 0, 0x98, 0x67);
1137
1138 /* reset device ident */
1139 pci_config_set_vendor_id(s->pci.dev.config, PCI_VENDOR_ID_AMD);
1140 pci_config_set_prog_interface(s->pci.dev.config, 00);
1141 pci_config_set_device_id(s->pci.dev.config, s->devid);
1142 pci_config_set_class(s->pci.dev.config, 0x0806);
1143
1144 /* reset AMDVI specific capabilities, all r/o */
1145 pci_set_long(s->pci.dev.config + s->capab_offset, AMDVI_CAPAB_FEATURES);
1146 pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_BAR_LOW,
1147 s->mmio.addr & ~(0xffff0000));
1148 pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_BAR_HIGH,
1149 (s->mmio.addr & ~(0xffff)) >> 16);
1150 pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_RANGE,
1151 0xff000000);
1152 pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_MISC, 0);
1153 pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_MISC,
1154 AMDVI_MAX_PH_ADDR | AMDVI_MAX_GVA_ADDR | AMDVI_MAX_VA_ADDR);
1155}
1156
1157static void amdvi_reset(DeviceState *dev)
1158{
1159 AMDVIState *s = AMD_IOMMU_DEVICE(dev);
1160
1161 msi_reset(&s->pci.dev);
1162 amdvi_init(s);
1163}
1164
1165static void amdvi_realize(DeviceState *dev, Error **err)
1166{
1d5b128c 1167 int ret = 0;
d29a09ca 1168 AMDVIState *s = AMD_IOMMU_DEVICE(dev);
fb9f5926 1169 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
ef0e8fc7 1170 MachineState *ms = MACHINE(qdev_get_machine());
29396ed9
MG
1171 PCMachineState *pcms = PC_MACHINE(ms);
1172 PCIBus *bus = pcms->bus;
ef0e8fc7 1173
d29a09ca
DK
1174 s->iotlb = g_hash_table_new_full(amdvi_uint64_hash,
1175 amdvi_uint64_equal, g_free, g_free);
1176
1177 /* This device should take care of IOMMU PCI properties */
fb9f5926 1178 x86_iommu->type = TYPE_AMD;
d29a09ca
DK
1179 qdev_set_parent_bus(DEVICE(&s->pci), &bus->qbus);
1180 object_property_set_bool(OBJECT(&s->pci), true, "realized", err);
9a7c2a59
MZ
1181 ret = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,
1182 AMDVI_CAPAB_SIZE, err);
1183 if (ret < 0) {
1184 return;
1185 }
1186 s->capab_offset = ret;
1187
1188 ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0,
1189 AMDVI_CAPAB_REG_SIZE, err);
1190 if (ret < 0) {
1191 return;
1192 }
1193 ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0,
1194 AMDVI_CAPAB_REG_SIZE, err);
1195 if (ret < 0) {
1196 return;
1197 }
d29a09ca
DK
1198
1199 /* set up MMIO */
1200 memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
1201 AMDVI_MMIO_SIZE);
1202
1203 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio);
1204 sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, AMDVI_BASE_ADDR);
1205 pci_setup_iommu(bus, amdvi_host_dma_iommu, s);
1206 s->devid = object_property_get_int(OBJECT(&s->pci), "addr", err);
1207 msi_init(&s->pci.dev, 0, 1, true, false, err);
1208 amdvi_init(s);
1209}
1210
1211static const VMStateDescription vmstate_amdvi = {
1212 .name = "amd-iommu",
1213 .unmigratable = 1
1214};
1215
1216static void amdvi_instance_init(Object *klass)
1217{
1218 AMDVIState *s = AMD_IOMMU_DEVICE(klass);
1219
1220 object_initialize(&s->pci, sizeof(s->pci), TYPE_AMD_IOMMU_PCI);
1221}
1222
1223static void amdvi_class_init(ObjectClass *klass, void* data)
1224{
1225 DeviceClass *dc = DEVICE_CLASS(klass);
1226 X86IOMMUClass *dc_class = X86_IOMMU_CLASS(klass);
1227
1228 dc->reset = amdvi_reset;
1229 dc->vmsd = &vmstate_amdvi;
1230 dc->hotpluggable = false;
1231 dc_class->realize = amdvi_realize;
8ab5700c 1232 /* Supported by the pc-q35-* machine types */
e4f4fb1e 1233 dc->user_creatable = true;
d29a09ca
DK
1234}
1235
1236static const TypeInfo amdvi = {
1237 .name = TYPE_AMD_IOMMU_DEVICE,
1238 .parent = TYPE_X86_IOMMU_DEVICE,
1239 .instance_size = sizeof(AMDVIState),
1240 .instance_init = amdvi_instance_init,
1241 .class_init = amdvi_class_init
1242};
1243
1244static const TypeInfo amdviPCI = {
1245 .name = "AMDVI-PCI",
1246 .parent = TYPE_PCI_DEVICE,
1247 .instance_size = sizeof(AMDVIPCIState),
fd3b02c8
EH
1248 .interfaces = (InterfaceInfo[]) {
1249 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1250 { },
1251 },
d29a09ca
DK
1252};
1253
1221a474
AK
1254static void amdvi_iommu_memory_region_class_init(ObjectClass *klass, void *data)
1255{
1256 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
1257
1258 imrc->translate = amdvi_translate;
1259 imrc->notify_flag_changed = amdvi_iommu_notify_flag_changed;
1260}
1261
1262static const TypeInfo amdvi_iommu_memory_region_info = {
1263 .parent = TYPE_IOMMU_MEMORY_REGION,
1264 .name = TYPE_AMD_IOMMU_MEMORY_REGION,
1265 .class_init = amdvi_iommu_memory_region_class_init,
1266};
1267
d29a09ca
DK
1268static void amdviPCI_register_types(void)
1269{
1270 type_register_static(&amdviPCI);
1271 type_register_static(&amdvi);
1221a474 1272 type_register_static(&amdvi_iommu_memory_region_info);
d29a09ca
DK
1273}
1274
1275type_init(amdviPCI_register_types);
This page took 0.253708 seconds and 4 git commands to generate.