pci_common.c 13 KB

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