pci_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. if (config_out_of_range(pbm, bus, devfn, where)) {
  261. /* Do nothing. */
  262. } else {
  263. /* We don't check for hypervisor errors here, but perhaps
  264. * we should and influence our return value depending upon
  265. * what kind of error is thrown.
  266. */
  267. pci_sun4v_config_put(devhandle,
  268. HV_PCI_DEVICE_BUILD(bus, device, func),
  269. where, size, value);
  270. }
  271. return PCIBIOS_SUCCESSFUL;
  272. }
  273. struct pci_ops sun4v_pci_ops = {
  274. .read = sun4v_read_pci_cfg,
  275. .write = sun4v_write_pci_cfg,
  276. };
  277. void pci_get_pbm_props(struct pci_pbm_info *pbm)
  278. {
  279. const u32 *val = of_get_property(pbm->op->dev.of_node, "bus-range", NULL);
  280. pbm->pci_first_busno = val[0];
  281. pbm->pci_last_busno = val[1];
  282. val = of_get_property(pbm->op->dev.of_node, "ino-bitmap", NULL);
  283. if (val) {
  284. pbm->ino_bitmap = (((u64)val[1] << 32UL) |
  285. ((u64)val[0] << 0UL));
  286. }
  287. }
  288. static void pci_register_legacy_regions(struct resource *io_res,
  289. struct resource *mem_res)
  290. {
  291. struct resource *p;
  292. /* VGA Video RAM. */
  293. p = kzalloc(sizeof(*p), GFP_KERNEL);
  294. if (!p)
  295. return;
  296. p->name = "Video RAM area";
  297. p->start = mem_res->start + 0xa0000UL;
  298. p->end = p->start + 0x1ffffUL;
  299. p->flags = IORESOURCE_BUSY;
  300. request_resource(mem_res, p);
  301. p = kzalloc(sizeof(*p), GFP_KERNEL);
  302. if (!p)
  303. return;
  304. p->name = "System ROM";
  305. p->start = mem_res->start + 0xf0000UL;
  306. p->end = p->start + 0xffffUL;
  307. p->flags = IORESOURCE_BUSY;
  308. request_resource(mem_res, p);
  309. p = kzalloc(sizeof(*p), GFP_KERNEL);
  310. if (!p)
  311. return;
  312. p->name = "Video ROM";
  313. p->start = mem_res->start + 0xc0000UL;
  314. p->end = p->start + 0x7fffUL;
  315. p->flags = IORESOURCE_BUSY;
  316. request_resource(mem_res, p);
  317. }
  318. static void pci_register_iommu_region(struct pci_pbm_info *pbm)
  319. {
  320. const u32 *vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma",
  321. NULL);
  322. if (vdma) {
  323. struct resource *rp = kzalloc(sizeof(*rp), GFP_KERNEL);
  324. if (!rp) {
  325. pr_info("%s: Cannot allocate IOMMU resource.\n",
  326. pbm->name);
  327. return;
  328. }
  329. rp->name = "IOMMU";
  330. rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
  331. rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
  332. rp->flags = IORESOURCE_BUSY;
  333. if (request_resource(&pbm->mem_space, rp)) {
  334. pr_info("%s: Unable to request IOMMU resource.\n",
  335. pbm->name);
  336. kfree(rp);
  337. }
  338. }
  339. }
  340. void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
  341. {
  342. const struct linux_prom_pci_ranges *pbm_ranges;
  343. int i, saw_mem, saw_io;
  344. int num_pbm_ranges;
  345. saw_mem = saw_io = 0;
  346. pbm_ranges = of_get_property(pbm->op->dev.of_node, "ranges", &i);
  347. if (!pbm_ranges) {
  348. prom_printf("PCI: Fatal error, missing PBM ranges property "
  349. " for %s\n",
  350. pbm->name);
  351. prom_halt();
  352. }
  353. num_pbm_ranges = i / sizeof(*pbm_ranges);
  354. for (i = 0; i < num_pbm_ranges; i++) {
  355. const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
  356. unsigned long a, size;
  357. u32 parent_phys_hi, parent_phys_lo;
  358. u32 size_hi, size_lo;
  359. int type;
  360. parent_phys_hi = pr->parent_phys_hi;
  361. parent_phys_lo = pr->parent_phys_lo;
  362. if (tlb_type == hypervisor)
  363. parent_phys_hi &= 0x0fffffff;
  364. size_hi = pr->size_hi;
  365. size_lo = pr->size_lo;
  366. type = (pr->child_phys_hi >> 24) & 0x3;
  367. a = (((unsigned long)parent_phys_hi << 32UL) |
  368. ((unsigned long)parent_phys_lo << 0UL));
  369. size = (((unsigned long)size_hi << 32UL) |
  370. ((unsigned long)size_lo << 0UL));
  371. switch (type) {
  372. case 0:
  373. /* PCI config space, 16MB */
  374. pbm->config_space = a;
  375. break;
  376. case 1:
  377. /* 16-bit IO space, 16MB */
  378. pbm->io_space.start = a;
  379. pbm->io_space.end = a + size - 1UL;
  380. pbm->io_space.flags = IORESOURCE_IO;
  381. saw_io = 1;
  382. break;
  383. case 2:
  384. /* 32-bit MEM space, 2GB */
  385. pbm->mem_space.start = a;
  386. pbm->mem_space.end = a + size - 1UL;
  387. pbm->mem_space.flags = IORESOURCE_MEM;
  388. saw_mem = 1;
  389. break;
  390. case 3:
  391. /* XXX 64-bit MEM handling XXX */
  392. default:
  393. break;
  394. }
  395. }
  396. if (!saw_io || !saw_mem) {
  397. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  398. pbm->name,
  399. (!saw_io ? "IO" : "MEM"));
  400. prom_halt();
  401. }
  402. printk("%s: PCI IO[%llx] MEM[%llx]\n",
  403. pbm->name,
  404. pbm->io_space.start,
  405. pbm->mem_space.start);
  406. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  407. request_resource(&ioport_resource, &pbm->io_space);
  408. request_resource(&iomem_resource, &pbm->mem_space);
  409. pci_register_legacy_regions(&pbm->io_space,
  410. &pbm->mem_space);
  411. pci_register_iommu_region(pbm);
  412. }
  413. /* Generic helper routines for PCI error reporting. */
  414. void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
  415. struct pci_bus *pbus)
  416. {
  417. struct pci_dev *pdev;
  418. struct pci_bus *bus;
  419. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  420. u16 status, error_bits;
  421. pci_read_config_word(pdev, PCI_STATUS, &status);
  422. error_bits =
  423. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  424. PCI_STATUS_REC_TARGET_ABORT));
  425. if (error_bits) {
  426. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  427. printk("%s: Device %s saw Target Abort [%016x]\n",
  428. pbm->name, pci_name(pdev), status);
  429. }
  430. }
  431. list_for_each_entry(bus, &pbus->children, node)
  432. pci_scan_for_target_abort(pbm, bus);
  433. }
  434. void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
  435. struct pci_bus *pbus)
  436. {
  437. struct pci_dev *pdev;
  438. struct pci_bus *bus;
  439. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  440. u16 status, error_bits;
  441. pci_read_config_word(pdev, PCI_STATUS, &status);
  442. error_bits =
  443. (status & (PCI_STATUS_REC_MASTER_ABORT));
  444. if (error_bits) {
  445. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  446. printk("%s: Device %s received Master Abort [%016x]\n",
  447. pbm->name, pci_name(pdev), status);
  448. }
  449. }
  450. list_for_each_entry(bus, &pbus->children, node)
  451. pci_scan_for_master_abort(pbm, bus);
  452. }
  453. void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
  454. struct pci_bus *pbus)
  455. {
  456. struct pci_dev *pdev;
  457. struct pci_bus *bus;
  458. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  459. u16 status, error_bits;
  460. pci_read_config_word(pdev, PCI_STATUS, &status);
  461. error_bits =
  462. (status & (PCI_STATUS_PARITY |
  463. PCI_STATUS_DETECTED_PARITY));
  464. if (error_bits) {
  465. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  466. printk("%s: Device %s saw Parity Error [%016x]\n",
  467. pbm->name, pci_name(pdev), status);
  468. }
  469. }
  470. list_for_each_entry(bus, &pbus->children, node)
  471. pci_scan_for_parity_error(pbm, bus);
  472. }