aboutsummaryrefslogtreecommitdiffstats
path: root/xen/arch/x86/hvm/svm/amd_iommu/pci-amd-iommu.c
blob: 7caa276f7354b6294549063df9c5b214121aa8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 * Copyright (C) 2007 Advanced Micro Devices, Inc.
 * Author: Leo Duran <leo.duran@amd.com>
 * Author: Wei Wang <wei.wang2@amd.com> - adapted to xen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <asm/amd-iommu.h>
#include <asm/hvm/svm/amd-iommu-proto.h>
#include <xen/sched.h>
#include <asm/mm.h>
#include "pci-direct.h"
#include "pci_regs.h"

struct list_head amd_iommu_head;
long amd_iommu_poll_comp_wait = COMPLETION_WAIT_DEFAULT_POLLING_COUNT;
static long amd_iommu_cmd_buffer_entries = IOMMU_CMD_BUFFER_DEFAULT_ENTRIES;
int nr_amd_iommus = 0;

/* will set if amd-iommu HW is found */
int amd_iommu_enabled = 0;

static int enable_amd_iommu = 0;
boolean_param("enable_amd_iommu", enable_amd_iommu);

static void deallocate_domain_page_tables(struct hvm_iommu *hd)
{
    if ( hd->root_table )
        free_xenheap_page(hd->root_table);
}

static void deallocate_domain_resources(struct hvm_iommu *hd)
{
    deallocate_domain_page_tables(hd);
}

static void __init init_cleanup(void)
{
    struct amd_iommu *iommu;

    for_each_amd_iommu ( iommu )
        unmap_iommu_mmio_region(iommu);
}

static void __init deallocate_iommu_table_struct(
    struct table_struct *table)
{
    if ( table->buffer )
    {
        free_xenheap_pages(table->buffer,
                           get_order_from_bytes(table->alloc_size));
        table->buffer = NULL;
    }
}

static void __init deallocate_iommu_resources(struct amd_iommu *iommu)
{
    deallocate_iommu_table_struct(&iommu->dev_table);
    deallocate_iommu_table_struct(&iommu->cmd_buffer);;
}

static void __init detect_cleanup(void)
{
    struct amd_iommu *iommu, *next;

    list_for_each_entry_safe ( iommu, next, &amd_iommu_head, list )
    {
        list_del(&iommu->list);
        deallocate_iommu_resources(iommu);
        xfree(iommu);
    }
}

static int requestor_id_from_bdf(int bdf)
{
    /* HACK - HACK */
    /* account for possible 'aliasing' by parent device */
    return bdf;
}

static int __init allocate_iommu_table_struct(struct table_struct *table,
                                              const char *name)
{
    table->buffer = (void *) alloc_xenheap_pages(
        get_order_from_bytes(table->alloc_size));

    if ( !table->buffer )
    {
        dprintk(XENLOG_ERR, "AMD IOMMU: Error allocating %s\n", name);
        return -ENOMEM;
    }

    memset(table->buffer, 0, table->alloc_size);

    return 0;
}

static int __init allocate_iommu_resources(struct amd_iommu *iommu)
{
    /* allocate 'device table' on a 4K boundary */
    iommu->dev_table.alloc_size =
        PAGE_ALIGN(((iommu->last_downstream_bus + 1) *
                    IOMMU_DEV_TABLE_ENTRIES_PER_BUS) *
                   IOMMU_DEV_TABLE_ENTRY_SIZE);
    iommu->dev_table.entries =
        iommu->dev_table.alloc_size / IOMMU_DEV_TABLE_ENTRY_SIZE;

    if ( allocate_iommu_table_struct(&iommu->dev_table,
                                     "Device Table") != 0 )
        goto error_out;

    /* allocate 'command buffer' in power of 2 increments of 4K */
    iommu->cmd_buffer_tail = 0;
    iommu->cmd_buffer.alloc_size =
        PAGE_SIZE << get_order_from_bytes(
            PAGE_ALIGN(amd_iommu_cmd_buffer_entries *
                       IOMMU_CMD_BUFFER_ENTRY_SIZE));

    iommu->cmd_buffer.entries =
        iommu->cmd_buffer.alloc_size / IOMMU_CMD_BUFFER_ENTRY_SIZE;

    if ( allocate_iommu_table_struct(&iommu->cmd_buffer,
                                     "Command Buffer") != 0 )
        goto error_out;

    return 0;

 error_out:
    deallocate_iommu_resources(iommu);
    return -ENOMEM;
}

int iommu_detect_callback(u8 bus, u8 dev, u8 func, u8 cap_ptr)
{
    struct amd_iommu *iommu;

    iommu = (struct amd_iommu *) xmalloc(struct amd_iommu);
    if ( !iommu )
    {
        dprintk(XENLOG_ERR, "AMD IOMMU: Error allocating amd_iommu\n");
        return -ENOMEM;
    }
    memset(iommu, 0, sizeof(struct amd_iommu));
    spin_lock_init(&iommu->lock);

    /* get capability and topology information */
    if ( get_iommu_capabilities(bus, dev, func, cap_ptr, iommu) != 0 )
        goto error_out;
    if ( get_iommu_last_downstream_bus(iommu) != 0 )
        goto error_out;

    list_add_tail(&iommu->list, &amd_iommu_head);

    /* allocate resources for this IOMMU */
    if (allocate_iommu_resources(iommu) != 0)
        goto error_out;

    return 0;

 error_out:
    xfree(iommu);
    return -ENODEV;
}

static int __init amd_iommu_init(void)
{
    struct amd_iommu *iommu;
    unsigned long flags;

    for_each_amd_iommu ( iommu )
    {
        spin_lock_irqsave(&iommu->lock, flags);

        /* register IOMMU data strucures in MMIO space */
        if ( map_iommu_mmio_region(iommu) != 0 )
            goto error_out;
        register_iommu_dev_table_in_mmio_space(iommu);
        register_iommu_cmd_buffer_in_mmio_space(iommu);

        /* enable IOMMU translation services */
        enable_iommu(iommu);
        nr_amd_iommus++;

        spin_unlock_irqrestore(&iommu->lock, flags);
    }

    amd_iommu_enabled = 1;

    return 0;

 error_out:
    init_cleanup();
    return -ENODEV;
}

struct amd_iommu *find_iommu_for_device(int bus, int devfn)
{
    struct amd_iommu *iommu;

    for_each_amd_iommu ( iommu )
    {
        if ( bus == iommu->root_bus )
        {
            if ( (devfn >= iommu->first_devfn) &&
                 (devfn <= iommu->last_devfn) )
                return iommu;
        }
        else if ( bus <= iommu->last_downstream_bus )
        {
            if ( iommu->downstream_bus_present[bus] )
                return iommu;
        }
    }

    return NULL;
}

void amd_iommu_setup_domain_device(
    struct domain *domain, struct amd_iommu *iommu, int requestor_id)
{
    void *dte;
    u64 root_ptr;
    unsigned long flags;
    struct hvm_iommu *hd = domain_hvm_iommu(domain);

    BUG_ON( !hd->root_table||!hd->paging_mode );

    root_ptr = (u64)virt_to_maddr(hd->root_table);
    dte = iommu->dev_table.buffer +
        (requestor_id * IOMMU_DEV_TABLE_ENTRY_SIZE);

    if ( !amd_iommu_is_dte_page_translation_valid((u32 *)dte) )
    {
        spin_lock_irqsave(&iommu->lock, flags); 

        amd_iommu_set_dev_table_entry(
            (u32 *)dte,
            root_ptr, hd->domain_id, hd->paging_mode);
        invalidate_dev_table_entry(iommu, requestor_id);
        flush_command_buffer(iommu);
        dprintk(XENLOG_INFO, "AMD IOMMU: Set DTE req_id:%x, "
                "root_ptr:%"PRIx64", domain_id:%d, paging_mode:%d\n",
                requestor_id, root_ptr, hd->domain_id, hd->paging_mode);

        spin_unlock_irqrestore(&iommu->lock, flags);
    }
}

void __init amd_iommu_setup_dom0_devices(void)
{
    struct hvm_iommu *hd = domain_hvm_iommu(dom0);
    struct amd_iommu *iommu;
    struct pci_dev *pdev;
    int bus, dev, func;
    u32 l;
    int req_id, bdf;

    for ( bus = 0; bus < 256; bus++ )
    {
        for ( dev = 0; dev < 32; dev++ )
        {
            for ( func = 0; func < 8; func++ )
            {
                l = read_pci_config(bus, dev, func, PCI_VENDOR_ID);
                /* some broken boards return 0 or ~0 if a slot is empty: */
                if ( l == 0xffffffff || l == 0x00000000 ||
                     l == 0x0000ffff || l == 0xffff0000 )
                    continue;

                pdev = xmalloc(struct pci_dev);
                pdev->bus = bus;
                pdev->devfn = PCI_DEVFN(dev, func);
                list_add_tail(&pdev->list, &hd->pdev_list);

                bdf = (bus << 8) | pdev->devfn;
                req_id = requestor_id_from_bdf(bdf);
                iommu = find_iommu_for_device(bus, pdev->devfn);

                if ( iommu )
                    amd_iommu_setup_domain_device(dom0, iommu, req_id);
            }
        }
    }
}

int amd_iommu_detect(void)
{
    unsigned long i;

    if ( !enable_amd_iommu )
    {
        printk("AMD IOMMU: Disabled\n");
        return 0;
    }

    INIT_LIST_HEAD(&amd_iommu_head);

    if ( scan_for_iommu(iommu_detect_callback) != 0 )
    {
        dprintk(XENLOG_ERR, "AMD IOMMU: Error detection\n");
        goto error_out;
    }

    if ( !iommu_found() )
    {
        printk("AMD IOMMU: Not found!\n");
        return 0;
    }

    if ( amd_iommu_init() != 0 )
    {
        dprintk(XENLOG_ERR, "AMD IOMMU: Error initialization\n");
        goto error_out;
    }

    if ( iommu_domain_init(dom0) != 0 )
        goto error_out;

    /* setup 1:1 page table for dom0 */
    for ( i = 0; i < max_page; i++ )
        amd_iommu_map_page(dom0, i, i);

    amd_iommu_setup_dom0_devices();
    return 0;

 error_out:
    detect_cleanup();
    return -ENODEV;

}

static int allocate_domain_resources(struct hvm_iommu *hd)
{
    /* allocate root table */
    unsigned long flags;

    spin_lock_irqsave(&hd->mapping_lock, flags);
    if ( !hd->root_table )
    {
        hd->root_table = (void *)alloc_xenheap_page();
        if ( !hd->root_table )
            goto error_out;
        memset((u8*)hd->root_table, 0, PAGE_SIZE);
    }
    spin_unlock_irqrestore(&hd->mapping_lock, flags);

    return 0;
 error_out:
    spin_unlock_irqrestore(&hd->mapping_lock, flags);
    return -ENOMEM;
}

static int get_paging_mode(unsigned long entries)
{
    int level = 1;

    BUG_ON ( !max_page );

    if ( entries > max_page )
        entries = max_page;

    while ( entries > PTE_PER_TABLE_SIZE )
    {
        entries = PTE_PER_TABLE_ALIGN(entries) >> PTE_PER_TABLE_SHIFT;
        ++level;
        if ( level > 6 )
            return -ENOMEM;
    }

    dprintk(XENLOG_INFO, "AMD IOMMU: paging mode = %d\n", level);

    return level;
}

int amd_iommu_domain_init(struct domain *domain)
{
    struct hvm_iommu *hd = domain_hvm_iommu(domain);

    /* allocate page directroy */
    if ( allocate_domain_resources(hd) != 0 )
    {
        deallocate_domain_resources(hd);
        return -ENOMEM;
    }

    if ( is_hvm_domain(domain) )
        hd->paging_mode = IOMMU_PAGE_TABLE_LEVEL_4;
    else
        hd->paging_mode = get_paging_mode(max_page);

    hd->domain_id = domain->domain_id;

    return 0;
}

static void amd_iommu_disable_domain_device(
    struct domain *domain, struct amd_iommu *iommu, u16 requestor_id)
{
    void *dte;
    unsigned long flags;

    dte = iommu->dev_table.buffer +
        (requestor_id * IOMMU_DEV_TABLE_ENTRY_SIZE);

    if ( amd_iommu_is_dte_page_translation_valid((u32 *)dte) )
    {
        spin_lock_irqsave(&iommu->lock, flags); 
        memset (dte, 0, IOMMU_DEV_TABLE_ENTRY_SIZE);
        invalidate_dev_table_entry(iommu, requestor_id);
        flush_command_buffer(iommu);
        dprintk(XENLOG_INFO , "AMD IOMMU: disable DTE 0x%x,"
                " domain_id:%d, paging_mode:%d\n",
                requestor_id,  domain_hvm_iommu(domain)->domain_id,
                domain_hvm_iommu(domain)->paging_mode);
        spin_unlock_irqrestore(&iommu->lock, flags);
    }
}

extern void pdev_flr(u8 bus, u8 devfn);

static int reassign_device( struct domain *source, struct domain *target,
                            u8 bus, u8 devfn)
{
    struct hvm_iommu *source_hd = domain_hvm_iommu(source);
    struct hvm_iommu *target_hd = domain_hvm_iommu(target);
    struct pci_dev *pdev;
    struct amd_iommu *iommu;
    int req_id, bdf;
    unsigned long flags;

    for_each_pdev( source, pdev )
    {
        if ( (pdev->bus != bus) || (pdev->devfn != devfn) )
            continue;

        pdev->bus = bus;
        pdev->devfn = devfn;

        bdf = (bus << 8) | devfn;
        req_id = requestor_id_from_bdf(bdf);
        iommu = find_iommu_for_device(bus, devfn);

        if ( iommu )
        {
            amd_iommu_disable_domain_device(source, iommu, req_id);
            /* Move pci device from the source domain to target domain. */
            spin_lock_irqsave(&source_hd->iommu_list_lock, flags);
            spin_lock_irqsave(&target_hd->iommu_list_lock, flags);
            list_move(&pdev->list, &target_hd->pdev_list);
            spin_unlock_irqrestore(&target_hd->iommu_list_lock, flags);
            spin_unlock_irqrestore(&source_hd->iommu_list_lock, flags);

            amd_iommu_setup_domain_device(target, iommu, req_id);
            gdprintk(XENLOG_INFO ,
                     "AMD IOMMU: reassign %x:%x.%x domain %d -> domain %d\n",
                     bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
                     source->domain_id, target->domain_id);
        }
        else
        {
            gdprintk(XENLOG_ERR , "AMD IOMMU: fail to find iommu."
                     " %x:%x.%x cannot be assigned to domain %d\n", 
                     bus, PCI_SLOT(devfn), PCI_FUNC(devfn), target->domain_id);
            return -ENODEV;
        }

        break;
    }
    return 0;
}

int amd_iommu_assign_device(struct domain *d, u8 bus, u8 devfn)
{
    pdev_flr(bus, devfn);
    return reassign_device(dom0, d, bus, devfn);
}

static void release_domain_devices(struct domain *d)
{
    struct hvm_iommu *hd  = domain_hvm_iommu(d);
    struct pci_dev *pdev;

    while ( !list_empty(&hd->pdev_list) )
    {
        pdev = list_entry(hd->pdev_list.next, typeof(*pdev), list);
        pdev_flr(pdev->bus, pdev->devfn);
        gdprintk(XENLOG_INFO ,
                 "AMD IOMMU: release devices %x:%x.%x\n",
                 pdev->bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
        reassign_device(d, dom0, pdev->bus, pdev->devfn);
    }
}

static void deallocate_next_page_table(void *table, unsigned long index,
                                       int level)
{
    unsigned long next_index;
    void *next_table, *pde;
    int next_level;

    pde = table + (index * IOMMU_PAGE_TABLE_ENTRY_SIZE);
    next_table = amd_iommu_get_vptr_from_page_table_entry((u32 *)pde);

    if ( next_table )
    {
        next_level = level - 1;
        if ( next_level > 1 )
        {
            next_index = 0;
            do
            {
                deallocate_next_page_table(next_table,
                                           next_index, next_level);
                ++next_index;
            } while (next_index < PTE_PER_TABLE_SIZE);
        }

        free_xenheap_page(next_table);
    }
}

static void deallocate_iommu_page_tables(struct domain *d)
{
    unsigned long index;
    struct hvm_iommu *hd  = domain_hvm_iommu(d);

    if ( hd ->root_table )
    {
        index = 0;
        do
        {
            deallocate_next_page_table(hd->root_table,
                                       index, hd->paging_mode);
            ++index;
        } while ( index < PTE_PER_TABLE_SIZE );

        free_xenheap_page(hd ->root_table);
    }

    hd ->root_table = NULL;
}

void amd_iommu_domain_destroy(struct domain *d)
{
    if ( !amd_iommu_enabled )
        return;

    deallocate_iommu_page_tables(d);
    release_domain_devices(d);
}

struct iommu_ops amd_iommu_ops = {
    .init = amd_iommu_domain_init,
    .assign_device  = amd_iommu_assign_device,
    .teardown = amd_iommu_domain_destroy,
    .map_page = amd_iommu_map_page,
    .unmap_page = amd_iommu_unmap_page,
};