pci_common.c 13 KB

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