pci_common.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* pci_common.c: PCI controller common support.
  2. *
  3. * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/string.h>
  6. #include <linux/slab.h>
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/device.h>
  10. #include <asm/prom.h>
  11. #include <asm/of_device.h>
  12. #include <asm/oplib.h>
  13. #include "pci_impl.h"
  14. #include "pci_sun4v.h"
  15. static int config_out_of_range(struct pci_pbm_info *pbm,
  16. unsigned long bus,
  17. unsigned long devfn,
  18. unsigned long reg)
  19. {
  20. if (bus < pbm->pci_first_busno ||
  21. bus > pbm->pci_last_busno)
  22. return 1;
  23. return 0;
  24. }
  25. static void *sun4u_config_mkaddr(struct pci_pbm_info *pbm,
  26. unsigned long bus,
  27. unsigned long devfn,
  28. unsigned long reg)
  29. {
  30. unsigned long rbits = pbm->config_space_reg_bits;
  31. if (config_out_of_range(pbm, bus, devfn, reg))
  32. return NULL;
  33. reg = (reg & ((1 << rbits) - 1));
  34. devfn <<= rbits;
  35. bus <<= rbits + 8;
  36. return (void *) (pbm->config_space | bus | devfn | reg);
  37. }
  38. static int sun4u_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  39. int where, int size, u32 *value)
  40. {
  41. struct pci_pbm_info *pbm = bus_dev->sysdata;
  42. unsigned char bus = bus_dev->number;
  43. u32 *addr;
  44. u16 tmp16;
  45. u8 tmp8;
  46. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  47. return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
  48. size, value);
  49. switch (size) {
  50. case 1:
  51. *value = 0xff;
  52. break;
  53. case 2:
  54. *value = 0xffff;
  55. break;
  56. case 4:
  57. *value = 0xffffffff;
  58. break;
  59. }
  60. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  61. if (!addr)
  62. return PCIBIOS_SUCCESSFUL;
  63. switch (size) {
  64. case 1:
  65. pci_config_read8((u8 *)addr, &tmp8);
  66. *value = (u32) tmp8;
  67. break;
  68. case 2:
  69. if (where & 0x01) {
  70. printk("pci_read_config_word: misaligned reg [%x]\n",
  71. where);
  72. return PCIBIOS_SUCCESSFUL;
  73. }
  74. pci_config_read16((u16 *)addr, &tmp16);
  75. *value = (u32) tmp16;
  76. break;
  77. case 4:
  78. if (where & 0x03) {
  79. printk("pci_read_config_dword: misaligned reg [%x]\n",
  80. where);
  81. return PCIBIOS_SUCCESSFUL;
  82. }
  83. pci_config_read32(addr, value);
  84. break;
  85. }
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. static int sun4u_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  89. int where, int size, u32 value)
  90. {
  91. struct pci_pbm_info *pbm = bus_dev->sysdata;
  92. unsigned char bus = bus_dev->number;
  93. u32 *addr;
  94. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  95. return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
  96. size, value);
  97. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  98. if (!addr)
  99. return PCIBIOS_SUCCESSFUL;
  100. switch (size) {
  101. case 1:
  102. pci_config_write8((u8 *)addr, value);
  103. break;
  104. case 2:
  105. if (where & 0x01) {
  106. printk("pci_write_config_word: misaligned reg [%x]\n",
  107. where);
  108. return PCIBIOS_SUCCESSFUL;
  109. }
  110. pci_config_write16((u16 *)addr, value);
  111. break;
  112. case 4:
  113. if (where & 0x03) {
  114. printk("pci_write_config_dword: misaligned reg [%x]\n",
  115. where);
  116. return PCIBIOS_SUCCESSFUL;
  117. }
  118. pci_config_write32(addr, value);
  119. }
  120. return PCIBIOS_SUCCESSFUL;
  121. }
  122. struct pci_ops sun4u_pci_ops = {
  123. .read = sun4u_read_pci_cfg,
  124. .write = sun4u_write_pci_cfg,
  125. };
  126. static int sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  127. int where, int size, u32 *value)
  128. {
  129. struct pci_pbm_info *pbm = bus_dev->sysdata;
  130. u32 devhandle = pbm->devhandle;
  131. unsigned int bus = bus_dev->number;
  132. unsigned int device = PCI_SLOT(devfn);
  133. unsigned int func = PCI_FUNC(devfn);
  134. unsigned long ret;
  135. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  136. return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
  137. size, value);
  138. if (config_out_of_range(pbm, bus, devfn, where)) {
  139. ret = ~0UL;
  140. } else {
  141. ret = pci_sun4v_config_get(devhandle,
  142. HV_PCI_DEVICE_BUILD(bus, device, func),
  143. where, size);
  144. }
  145. switch (size) {
  146. case 1:
  147. *value = ret & 0xff;
  148. break;
  149. case 2:
  150. *value = ret & 0xffff;
  151. break;
  152. case 4:
  153. *value = ret & 0xffffffff;
  154. break;
  155. };
  156. return PCIBIOS_SUCCESSFUL;
  157. }
  158. static int sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  159. int where, int size, u32 value)
  160. {
  161. struct pci_pbm_info *pbm = bus_dev->sysdata;
  162. u32 devhandle = pbm->devhandle;
  163. unsigned int bus = bus_dev->number;
  164. unsigned int device = PCI_SLOT(devfn);
  165. unsigned int func = PCI_FUNC(devfn);
  166. unsigned long ret;
  167. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  168. return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
  169. size, value);
  170. if (config_out_of_range(pbm, bus, devfn, where)) {
  171. /* Do nothing. */
  172. } else {
  173. ret = pci_sun4v_config_put(devhandle,
  174. HV_PCI_DEVICE_BUILD(bus, device, func),
  175. where, size, value);
  176. }
  177. return PCIBIOS_SUCCESSFUL;
  178. }
  179. struct pci_ops sun4v_pci_ops = {
  180. .read = sun4v_read_pci_cfg,
  181. .write = sun4v_write_pci_cfg,
  182. };
  183. void pci_get_pbm_props(struct pci_pbm_info *pbm)
  184. {
  185. const u32 *val = of_get_property(pbm->prom_node, "bus-range", NULL);
  186. pbm->pci_first_busno = val[0];
  187. pbm->pci_last_busno = val[1];
  188. val = of_get_property(pbm->prom_node, "ino-bitmap", NULL);
  189. if (val) {
  190. pbm->ino_bitmap = (((u64)val[1] << 32UL) |
  191. ((u64)val[0] << 0UL));
  192. }
  193. }
  194. static void pci_register_legacy_regions(struct resource *io_res,
  195. struct resource *mem_res)
  196. {
  197. struct resource *p;
  198. /* VGA Video RAM. */
  199. p = kzalloc(sizeof(*p), GFP_KERNEL);
  200. if (!p)
  201. return;
  202. p->name = "Video RAM area";
  203. p->start = mem_res->start + 0xa0000UL;
  204. p->end = p->start + 0x1ffffUL;
  205. p->flags = IORESOURCE_BUSY;
  206. request_resource(mem_res, p);
  207. p = kzalloc(sizeof(*p), GFP_KERNEL);
  208. if (!p)
  209. return;
  210. p->name = "System ROM";
  211. p->start = mem_res->start + 0xf0000UL;
  212. p->end = p->start + 0xffffUL;
  213. p->flags = IORESOURCE_BUSY;
  214. request_resource(mem_res, p);
  215. p = kzalloc(sizeof(*p), GFP_KERNEL);
  216. if (!p)
  217. return;
  218. p->name = "Video ROM";
  219. p->start = mem_res->start + 0xc0000UL;
  220. p->end = p->start + 0x7fffUL;
  221. p->flags = IORESOURCE_BUSY;
  222. request_resource(mem_res, p);
  223. }
  224. static void pci_register_iommu_region(struct pci_pbm_info *pbm)
  225. {
  226. const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
  227. if (vdma) {
  228. struct resource *rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  229. if (!rp) {
  230. prom_printf("Cannot allocate IOMMU resource.\n");
  231. prom_halt();
  232. }
  233. rp->name = "IOMMU";
  234. rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
  235. rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
  236. rp->flags = IORESOURCE_BUSY;
  237. request_resource(&pbm->mem_space, rp);
  238. }
  239. }
  240. void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
  241. {
  242. const struct linux_prom_pci_ranges *pbm_ranges;
  243. int i, saw_mem, saw_io;
  244. int num_pbm_ranges;
  245. saw_mem = saw_io = 0;
  246. pbm_ranges = of_get_property(pbm->prom_node, "ranges", &i);
  247. num_pbm_ranges = i / sizeof(*pbm_ranges);
  248. for (i = 0; i < num_pbm_ranges; i++) {
  249. const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
  250. unsigned long a;
  251. u32 parent_phys_hi, parent_phys_lo;
  252. int type;
  253. parent_phys_hi = pr->parent_phys_hi;
  254. parent_phys_lo = pr->parent_phys_lo;
  255. if (tlb_type == hypervisor)
  256. parent_phys_hi &= 0x0fffffff;
  257. type = (pr->child_phys_hi >> 24) & 0x3;
  258. a = (((unsigned long)parent_phys_hi << 32UL) |
  259. ((unsigned long)parent_phys_lo << 0UL));
  260. switch (type) {
  261. case 0:
  262. /* PCI config space, 16MB */
  263. pbm->config_space = a;
  264. break;
  265. case 1:
  266. /* 16-bit IO space, 16MB */
  267. pbm->io_space.start = a;
  268. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  269. pbm->io_space.flags = IORESOURCE_IO;
  270. saw_io = 1;
  271. break;
  272. case 2:
  273. /* 32-bit MEM space, 2GB */
  274. pbm->mem_space.start = a;
  275. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  276. pbm->mem_space.flags = IORESOURCE_MEM;
  277. saw_mem = 1;
  278. break;
  279. case 3:
  280. /* XXX 64-bit MEM handling XXX */
  281. default:
  282. break;
  283. };
  284. }
  285. if (!saw_io || !saw_mem) {
  286. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  287. pbm->name,
  288. (!saw_io ? "IO" : "MEM"));
  289. prom_halt();
  290. }
  291. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  292. pbm->name,
  293. pbm->io_space.start,
  294. pbm->mem_space.start);
  295. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  296. request_resource(&ioport_resource, &pbm->io_space);
  297. request_resource(&iomem_resource, &pbm->mem_space);
  298. pci_register_legacy_regions(&pbm->io_space,
  299. &pbm->mem_space);
  300. pci_register_iommu_region(pbm);
  301. }
  302. /* Generic helper routines for PCI error reporting. */
  303. void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
  304. struct pci_bus *pbus)
  305. {
  306. struct pci_dev *pdev;
  307. struct pci_bus *bus;
  308. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  309. u16 status, error_bits;
  310. pci_read_config_word(pdev, PCI_STATUS, &status);
  311. error_bits =
  312. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  313. PCI_STATUS_REC_TARGET_ABORT));
  314. if (error_bits) {
  315. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  316. printk("%s: Device %s saw Target Abort [%016x]\n",
  317. pbm->name, pci_name(pdev), status);
  318. }
  319. }
  320. list_for_each_entry(bus, &pbus->children, node)
  321. pci_scan_for_target_abort(pbm, bus);
  322. }
  323. void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
  324. struct pci_bus *pbus)
  325. {
  326. struct pci_dev *pdev;
  327. struct pci_bus *bus;
  328. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  329. u16 status, error_bits;
  330. pci_read_config_word(pdev, PCI_STATUS, &status);
  331. error_bits =
  332. (status & (PCI_STATUS_REC_MASTER_ABORT));
  333. if (error_bits) {
  334. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  335. printk("%s: Device %s received Master Abort [%016x]\n",
  336. pbm->name, pci_name(pdev), status);
  337. }
  338. }
  339. list_for_each_entry(bus, &pbus->children, node)
  340. pci_scan_for_master_abort(pbm, bus);
  341. }
  342. void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
  343. struct pci_bus *pbus)
  344. {
  345. struct pci_dev *pdev;
  346. struct pci_bus *bus;
  347. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  348. u16 status, error_bits;
  349. pci_read_config_word(pdev, PCI_STATUS, &status);
  350. error_bits =
  351. (status & (PCI_STATUS_PARITY |
  352. PCI_STATUS_DETECTED_PARITY));
  353. if (error_bits) {
  354. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  355. printk("%s: Device %s saw Parity Error [%016x]\n",
  356. pbm->name, pci_name(pdev), status);
  357. }
  358. }
  359. list_for_each_entry(bus, &pbus->children, node)
  360. pci_scan_for_parity_error(pbm, bus);
  361. }