Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * platform-pci.c |
| 3 | * |
| 4 | * Xen platform PCI device driver |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 5 | * |
| 6 | * Authors: ssmith@xensource.com and stefano.stabellini@eu.citrix.com |
| 7 | * |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 8 | * Copyright (c) 2005, Intel Corporation. |
| 9 | * Copyright (c) 2007, XenSource Inc. |
| 10 | * Copyright (c) 2010, Citrix |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms and conditions of the GNU General Public License, |
| 14 | * version 2, as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 19 | * more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 23 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/io.h> |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 30 | #include <linux/init.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 31 | #include <linux/pci.h> |
| 32 | |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 33 | #include <xen/platform_pci.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 34 | #include <xen/grant_table.h> |
| 35 | #include <xen/xenbus.h> |
| 36 | #include <xen/events.h> |
| 37 | #include <xen/hvm.h> |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 38 | #include <xen/xen-ops.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 39 | |
| 40 | #define DRV_NAME "xen-platform-pci" |
| 41 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 42 | static unsigned long platform_mmio; |
| 43 | static unsigned long platform_mmio_alloc; |
| 44 | static unsigned long platform_mmiolen; |
| 45 | |
Rashika Kheria | 598ddb8 | 2014-02-09 16:29:30 +0530 | [diff] [blame] | 46 | static unsigned long alloc_xen_mmio(unsigned long len) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 47 | { |
| 48 | unsigned long addr; |
| 49 | |
| 50 | addr = platform_mmio + platform_mmio_alloc; |
| 51 | platform_mmio_alloc += len; |
| 52 | BUG_ON(platform_mmio_alloc > platform_mmiolen); |
| 53 | |
| 54 | return addr; |
| 55 | } |
| 56 | |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 57 | static int platform_pci_probe(struct pci_dev *pdev, |
| 58 | const struct pci_device_id *ent) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 59 | { |
| 60 | int i, ret; |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 61 | long ioaddr; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 62 | long mmio_addr, mmio_len; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 63 | unsigned int max_nr_gframes; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 64 | unsigned long grant_frames; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 65 | |
Olaf Hering | 38ad4f4 | 2012-07-10 15:31:39 +0200 | [diff] [blame] | 66 | if (!xen_domain()) |
| 67 | return -ENODEV; |
| 68 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 69 | i = pci_enable_device(pdev); |
| 70 | if (i) |
| 71 | return i; |
| 72 | |
| 73 | ioaddr = pci_resource_start(pdev, 0); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 74 | |
| 75 | mmio_addr = pci_resource_start(pdev, 1); |
| 76 | mmio_len = pci_resource_len(pdev, 1); |
| 77 | |
| 78 | if (mmio_addr == 0 || ioaddr == 0) { |
| 79 | dev_err(&pdev->dev, "no resources found\n"); |
| 80 | ret = -ENOENT; |
| 81 | goto pci_out; |
| 82 | } |
| 83 | |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 84 | ret = pci_request_region(pdev, 1, DRV_NAME); |
| 85 | if (ret < 0) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 86 | goto pci_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 87 | |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 88 | ret = pci_request_region(pdev, 0, DRV_NAME); |
| 89 | if (ret < 0) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 90 | goto mem_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 91 | |
| 92 | platform_mmio = mmio_addr; |
| 93 | platform_mmiolen = mmio_len; |
| 94 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 95 | max_nr_gframes = gnttab_max_grant_frames(); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 96 | grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes); |
Wei Yongjun | 89b9e08 | 2014-01-07 21:11:05 +0800 | [diff] [blame] | 97 | ret = gnttab_setup_auto_xlat_frames(grant_frames); |
| 98 | if (ret) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 99 | goto out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 100 | ret = gnttab_init(); |
| 101 | if (ret) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 102 | goto grant_out; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 103 | xenbus_probe(NULL); |
| 104 | return 0; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 105 | grant_out: |
| 106 | gnttab_free_auto_xlat_frames(); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 107 | out: |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 108 | pci_release_region(pdev, 0); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 109 | mem_out: |
Ian Campbell | 00f28e4 | 2011-01-11 11:50:28 +0000 | [diff] [blame] | 110 | pci_release_region(pdev, 1); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 111 | pci_out: |
| 112 | pci_disable_device(pdev); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 116 | static struct pci_device_id platform_pci_tbl[] = { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 117 | {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM, |
| 118 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 119 | {0,} |
| 120 | }; |
| 121 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 122 | static struct pci_driver platform_driver = { |
| 123 | .name = DRV_NAME, |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 124 | .probe = platform_pci_probe, |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 125 | .id_table = platform_pci_tbl, |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 128 | static int __init platform_pci_init(void) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 129 | { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 130 | return pci_register_driver(&platform_driver); |
| 131 | } |
Paul Gortmaker | e01dc53 | 2016-02-21 19:06:08 -0500 | [diff] [blame] | 132 | device_initcall(platform_pci_init); |