aboutsummaryrefslogtreecommitdiffstats
path: root/xen/arch/x86/hvm/svm/amd_iommu/pci-amd-iommu.c
blob: 8b99a78257a4771ee3cdb81e79cf4ec5701eb22d (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
/*
 * 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;

    dprintk(XENLOG_ERR, "AMD IOMMU: %s()\n", __FUNCTION__);

    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;

    dprintk(XENLOG_ERR, "AMD IOMMU: %s()\n", __FUNCTION__);

    for_each_amd_iommu(iommu) {
        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);

    spin_lock_irqsave(&iommu->lock, flags); 

    amd_iommu_set_dev_table_entry((u32 *)dte,
        root_ptr, hd->domain_id, hd->paging_mode);

    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 ( amd_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 */
    hd->root_table = (void *)alloc_xenheap_page();
    if ( !hd->root_table )
        return -ENOMEM;
    memset((u8*)hd->root_table, 0, PAGE_SIZE);

    return 0;
}

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);

    spin_lock_init(&hd->mapping_lock);
    spin_lock_init(&hd->iommu_list_lock);
    INIT_LIST_HEAD(&hd->pdev_list);

    /* allocate page directroy */
    if ( allocate_domain_resources(hd) != 0 ) {
        dprintk(XENLOG_ERR, "AMD IOMMU: %s()\n", __FUNCTION__);
        goto error_out;
    }

    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;

error_out:
    deallocate_domain_resources(hd);
    return -ENOMEM;
}