pci_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <linux/of_device.h>
  11. #include <asm/prom.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. /* At least on Sabre, it is necessary to access all PCI host controller
  39. * registers at their natural size, otherwise zeros are returned.
  40. * Strange but true, and I see no language in the UltraSPARC-IIi
  41. * programmer's manual that mentions this even indirectly.
  42. */
  43. static int sun4u_read_pci_cfg_host(struct pci_pbm_info *pbm,
  44. unsigned char bus, unsigned int devfn,
  45. int where, int size, u32 *value)
  46. {
  47. u32 tmp32, *addr;
  48. u16 tmp16;
  49. u8 tmp8;
  50. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  51. if (!addr)
  52. return PCIBIOS_SUCCESSFUL;
  53. switch (size) {
  54. case 1:
  55. if (where < 8) {
  56. unsigned long align = (unsigned long) addr;
  57. align &= ~1;
  58. pci_config_read16((u16 *)align, &tmp16);
  59. if (where & 1)
  60. *value = tmp16 >> 8;
  61. else
  62. *value = tmp16 & 0xff;
  63. } else {
  64. pci_config_read8((u8 *)addr, &tmp8);
  65. *value = (u32) tmp8;
  66. }
  67. break;
  68. case 2:
  69. if (where < 8) {
  70. pci_config_read16((u16 *)addr, &tmp16);
  71. *value = (u32) tmp16;
  72. } else {
  73. pci_config_read8((u8 *)addr, &tmp8);
  74. *value = (u32) tmp8;
  75. pci_config_read8(((u8 *)addr) + 1, &tmp8);
  76. *value |= ((u32) tmp8) << 8;
  77. }
  78. break;
  79. case 4:
  80. tmp32 = 0xffffffff;
  81. sun4u_read_pci_cfg_host(pbm, bus, devfn,
  82. where, 2, &tmp32);
  83. *value = tmp32;
  84. tmp32 = 0xffffffff;
  85. sun4u_read_pci_cfg_host(pbm, bus, devfn,
  86. where + 2, 2, &tmp32);
  87. *value |= tmp32 << 16;
  88. break;
  89. }
  90. return PCIBIOS_SUCCESSFUL;
  91. }
  92. static int sun4u_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  93. int where, int size, u32 *value)
  94. {
  95. struct pci_pbm_info *pbm = bus_dev->sysdata;
  96. unsigned char bus = bus_dev->number;
  97. u32 *addr;
  98. u16 tmp16;
  99. u8 tmp8;
  100. switch (size) {
  101. case 1:
  102. *value = 0xff;
  103. break;
  104. case 2:
  105. *value = 0xffff;
  106. break;
  107. case 4:
  108. *value = 0xffffffff;
  109. break;
  110. }
  111. if (!bus_dev->number && !PCI_SLOT(devfn))
  112. return sun4u_read_pci_cfg_host(pbm, bus, devfn, where,
  113. size, value);
  114. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  115. if (!addr)
  116. return PCIBIOS_SUCCESSFUL;
  117. switch (size) {
  118. case 1:
  119. pci_config_read8((u8 *)addr, &tmp8);
  120. *value = (u32) tmp8;
  121. break;
  122. case 2:
  123. if (where & 0x01) {
  124. printk("pci_read_config_word: misaligned reg [%x]\n",
  125. where);
  126. return PCIBIOS_SUCCESSFUL;
  127. }
  128. pci_config_read16((u16 *)addr, &tmp16);
  129. *value = (u32) tmp16;
  130. break;
  131. case 4:
  132. if (where & 0x03) {
  133. printk("pci_read_config_dword: misaligned reg [%x]\n",
  134. where);
  135. return PCIBIOS_SUCCESSFUL;
  136. }
  137. pci_config_read32(addr, value);
  138. break;
  139. }
  140. return PCIBIOS_SUCCESSFUL;
  141. }
  142. static int sun4u_write_pci_cfg_host(struct pci_pbm_info *pbm,
  143. unsigned char bus, unsigned int devfn,
  144. int where, int size, u32 value)
  145. {
  146. u32 *addr;
  147. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  148. if (!addr)
  149. return PCIBIOS_SUCCESSFUL;
  150. switch (size) {
  151. case 1:
  152. if (where < 8) {
  153. unsigned long align = (unsigned long) addr;
  154. u16 tmp16;
  155. align &= ~1;
  156. pci_config_read16((u16 *)align, &tmp16);
  157. if (where & 1) {
  158. tmp16 &= 0x00ff;
  159. tmp16 |= value << 8;
  160. } else {
  161. tmp16 &= 0xff00;
  162. tmp16 |= value;
  163. }
  164. pci_config_write16((u16 *)align, tmp16);
  165. } else
  166. pci_config_write8((u8 *)addr, value);
  167. break;
  168. case 2:
  169. if (where < 8) {
  170. pci_config_write16((u16 *)addr, value);
  171. } else {
  172. pci_config_write8((u8 *)addr, value & 0xff);
  173. pci_config_write8(((u8 *)addr) + 1, value >> 8);
  174. }
  175. break;
  176. case 4:
  177. sun4u_write_pci_cfg_host(pbm, bus, devfn,
  178. where, 2, value & 0xffff);
  179. sun4u_write_pci_cfg_host(pbm, bus, devfn,
  180. where + 2, 2, value >> 16);
  181. break;
  182. }
  183. return PCIBIOS_SUCCESSFUL;
  184. }
  185. static int sun4u_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  186. int where, int size, u32 value)
  187. {
  188. struct pci_pbm_info *pbm = bus_dev->sysdata;
  189. unsigned char bus = bus_dev->number;
  190. u32 *addr;
  191. if (!bus_dev->number && !PCI_SLOT(devfn))
  192. return sun4u_write_pci_cfg_host(pbm, bus, devfn, where,
  193. size, value);
  194. addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
  195. if (!addr)
  196. return PCIBIOS_SUCCESSFUL;
  197. switch (size) {
  198. case 1:
  199. pci_config_write8((u8 *)addr, value);
  200. break;
  201. case 2:
  202. if (where & 0x01) {
  203. printk("pci_write_config_word: misaligned reg [%x]\n",
  204. where);
  205. return PCIBIOS_SUCCESSFUL;
  206. }
  207. pci_config_write16((u16 *)addr, value);
  208. break;
  209. case 4:
  210. if (where & 0x03) {
  211. printk("pci_write_config_dword: misaligned reg [%x]\n",
  212. where);
  213. return PCIBIOS_SUCCESSFUL;
  214. }
  215. pci_config_write32(addr, value);
  216. }
  217. return PCIBIOS_SUCCESSFUL;
  218. }
  219. struct pci_ops sun4u_pci_ops = {
  220. .read = sun4u_read_pci_cfg,
  221. .write = sun4u_write_pci_cfg,
  222. };
  223. static int sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  224. int where, int size, u32 *value)
  225. {
  226. struct pci_pbm_info *pbm = bus_dev->sysdata;
  227. u32 devhandle = pbm->devhandle;
  228. unsigned int bus = bus_dev->number;
  229. unsigned int device = PCI_SLOT(devfn);
  230. unsigned int func = PCI_FUNC(devfn);
  231. unsigned long ret;
  232. if (config_out_of_range(pbm, bus, devfn, where)) {
  233. ret = ~0UL;
  234. } else {
  235. ret = pci_sun4v_config_get(devhandle,
  236. HV_PCI_DEVICE_BUILD(bus, device, func),
  237. where, size);
  238. }
  239. switch (size) {
  240. case 1:
  241. *value = ret & 0xff;
  242. break;
  243. case 2:
  244. *value = ret & 0xffff;
  245. break;
  246. case 4:
  247. *value = ret & 0xffffffff;
  248. break;
  249. };
  250. return PCIBIOS_SUCCESSFUL;
  251. }
  252. static int sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  253. int where, int size, u32 value)
  254. {
  255. struct pci_pbm_info *pbm = bus_dev->sysdata;
  256. u32 devhandle = pbm->devhandle;
  257. unsigned int bus = bus_dev->number;
  258. unsigned int device = PCI_SLOT(devfn);
  259. unsigned int func = PCI_FUNC(devfn);
  260. unsigned long ret;
  261. if (config_out_of_range(pbm, bus, devfn, where)) {
  262. /* Do nothing. */
  263. } else {
  264. ret = pci_sun4v_config_put(devhandle,
  265. HV_PCI_DEVICE_BUILD(bus, device, func),
  266. where, size, value);
  267. }
  268. return PCIBIOS_SUCCESSFUL;
  269. }
  270. struct pci_ops sun4v_pci_ops = {
  271. .read = sun4v_read_pci_cfg,
  272. .write = sun4v_write_pci_cfg,
  273. };
  274. void pci_get_pbm_props(struct pci_pbm_info *pbm)
  275. {
  276. const u32 *val = of_get_property(pbm->op->node, "bus-range", NULL);
  277. pbm->pci_first_busno = val[0];
  278. pbm->pci_last_busno = val[1];
  279. val = of_get_property(pbm->op->node, "ino-bitmap", NULL);
  280. if (val) {
  281. pbm->ino_bitmap = (((u64)val[1] << 32UL) |
  282. ((u64)val[0] << 0UL));
  283. }
  284. }
  285. static void pci_register_legacy_regions(struct resource *io_res,
  286. struct resource *mem_res)
  287. {
  288. struct resource *p;
  289. /* VGA Video RAM. */
  290. p = kzalloc(sizeof(*p), GFP_KERNEL);
  291. if (!p)
  292. return;
  293. p->name = "Video RAM area";
  294. p->start = mem_res->start + 0xa0000UL;
  295. p->end = p->start + 0x1ffffUL;
  296. p->flags = IORESOURCE_BUSY;
  297. request_resource(mem_res, p);
  298. p = kzalloc(sizeof(*p), GFP_KERNEL);
  299. if (!p)
  300. return;
  301. p->name = "System ROM";
  302. p->start = mem_res->start + 0xf0000UL;
  303. p->end = p->start + 0xffffUL;
  304. p->flags = IORESOURCE_BUSY;
  305. request_resource(mem_res, p);
  306. p = kzalloc(sizeof(*p), GFP_KERNEL);
  307. if (!p)
  308. return;
  309. p->name = "Video ROM";
  310. p->start = mem_res->start + 0xc0000UL;
  311. p->end = p->start + 0x7fffUL;
  312. p->flags = IORESOURCE_BUSY;
  313. request_resource(mem_res, p);
  314. }
  315. static void pci_register_iommu_region(struct pci_pbm_info *pbm)
  316. {
  317. const u32 *vdma = of_get_property(pbm->op->node, "virtual-dma", NULL);
  318. if (vdma) {
  319. struct resource *rp = kzalloc(sizeof(*rp), GFP_KERNEL);
  320. if (!rp) {
  321. prom_printf("Cannot allocate IOMMU resource.\n");
  322. prom_halt();
  323. }
  324. rp->name = "IOMMU";
  325. rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
  326. rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
  327. rp->flags = IORESOURCE_BUSY;
  328. request_resource(&pbm->mem_space, rp);
  329. }
  330. }
  331. void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
  332. {
  333. const struct linux_prom_pci_ranges *pbm_ranges;
  334. int i, saw_mem, saw_io;
  335. int num_pbm_ranges;
  336. saw_mem = saw_io = 0;
  337. pbm_ranges = of_get_property(pbm->op->node, "ranges", &i);
  338. if (!pbm_ranges) {
  339. prom_printf("PCI: Fatal error, missing PBM ranges property "
  340. " for %s\n",
  341. pbm->name);
  342. prom_halt();
  343. }
  344. num_pbm_ranges = i / sizeof(*pbm_ranges);
  345. for (i = 0; i < num_pbm_ranges; i++) {
  346. const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
  347. unsigned long a, size;
  348. u32 parent_phys_hi, parent_phys_lo;
  349. u32 size_hi, size_lo;
  350. int type;
  351. parent_phys_hi = pr->parent_phys_hi;
  352. parent_phys_lo = pr->parent_phys_lo;
  353. if (tlb_type == hypervisor)
  354. parent_phys_hi &= 0x0fffffff;
  355. size_hi = pr->size_hi;
  356. size_lo = pr->size_lo;
  357. type = (pr->child_phys_hi >> 24) & 0x3;
  358. a = (((unsigned long)parent_phys_hi << 32UL) |
  359. ((unsigned long)parent_phys_lo << 0UL));
  360. size = (((unsigned long)size_hi << 32UL) |
  361. ((unsigned long)size_lo << 0UL));
  362. switch (type) {
  363. case 0:
  364. /* PCI config space, 16MB */
  365. pbm->config_space = a;
  366. break;
  367. case 1:
  368. /* 16-bit IO space, 16MB */
  369. pbm->io_space.start = a;
  370. pbm->io_space.end = a + size - 1UL;
  371. pbm->io_space.flags = IORESOURCE_IO;
  372. saw_io = 1;
  373. break;
  374. case 2:
  375. /* 32-bit MEM space, 2GB */
  376. pbm->mem_space.start = a;
  377. pbm->mem_space.end = a + size - 1UL;
  378. pbm->mem_space.flags = IORESOURCE_MEM;
  379. saw_mem = 1;
  380. break;
  381. case 3:
  382. /* XXX 64-bit MEM handling XXX */
  383. default:
  384. break;
  385. };
  386. }
  387. if (!saw_io || !saw_mem) {
  388. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  389. pbm->name,
  390. (!saw_io ? "IO" : "MEM"));
  391. prom_halt();
  392. }
  393. printk("%s: PCI IO[%llx] MEM[%llx]\n",
  394. pbm->name,
  395. pbm->io_space.start,
  396. pbm->mem_space.start);
  397. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  398. request_resource(&ioport_resource, &pbm->io_space);
  399. request_resource(&iomem_resource, &pbm->mem_space);
  400. pci_register_legacy_regions(&pbm->io_space,
  401. &pbm->mem_space);
  402. pci_register_iommu_region(pbm);
  403. }
  404. /* Generic helper routines for PCI error reporting. */
  405. void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
  406. struct pci_bus *pbus)
  407. {
  408. struct pci_dev *pdev;
  409. struct pci_bus *bus;
  410. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  411. u16 status, error_bits;
  412. pci_read_config_word(pdev, PCI_STATUS, &status);
  413. error_bits =
  414. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  415. PCI_STATUS_REC_TARGET_ABORT));
  416. if (error_bits) {
  417. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  418. printk("%s: Device %s saw Target Abort [%016x]\n",
  419. pbm->name, pci_name(pdev), status);
  420. }
  421. }
  422. list_for_each_entry(bus, &pbus->children, node)
  423. pci_scan_for_target_abort(pbm, bus);
  424. }
  425. void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
  426. struct pci_bus *pbus)
  427. {
  428. struct pci_dev *pdev;
  429. struct pci_bus *bus;
  430. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  431. u16 status, error_bits;
  432. pci_read_config_word(pdev, PCI_STATUS, &status);
  433. error_bits =
  434. (status & (PCI_STATUS_REC_MASTER_ABORT));
  435. if (error_bits) {
  436. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  437. printk("%s: Device %s received Master Abort [%016x]\n",
  438. pbm->name, pci_name(pdev), status);
  439. }
  440. }
  441. list_for_each_entry(bus, &pbus->children, node)
  442. pci_scan_for_master_abort(pbm, bus);
  443. }
  444. void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
  445. struct pci_bus *pbus)
  446. {
  447. struct pci_dev *pdev;
  448. struct pci_bus *bus;
  449. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  450. u16 status, error_bits;
  451. pci_read_config_word(pdev, PCI_STATUS, &status);
  452. error_bits =
  453. (status & (PCI_STATUS_PARITY |
  454. PCI_STATUS_DETECTED_PARITY));
  455. if (error_bits) {
  456. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  457. printk("%s: Device %s saw Parity Error [%016x]\n",
  458. pbm->name, pci_name(pdev), status);
  459. }
  460. }
  461. list_for_each_entry(bus, &pbus->children, node)
  462. pci_scan_for_parity_error(pbm, bus);
  463. }