pci_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
  2. * pci_common.c: PCI controller common support.
  3. *
  4. * Copyright (C) 1999 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/string.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <asm/pbm.h>
  10. #include <asm/prom.h>
  11. #include "pci_impl.h"
  12. /* Pass "pci=irq_verbose" on the kernel command line to enable this. */
  13. int pci_irq_verbose;
  14. /* Fix self device of BUS and hook it into BUS->self.
  15. * The pci_scan_bus does not do this for the host bridge.
  16. */
  17. void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
  18. {
  19. struct pci_dev *pdev;
  20. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  21. if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
  22. pbus->self = pdev;
  23. return;
  24. }
  25. }
  26. prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
  27. prom_halt();
  28. }
  29. /* Find the OBP PROM device tree node for a PCI device. */
  30. static struct device_node * __init
  31. find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
  32. struct device_node *bus_node,
  33. struct linux_prom_pci_registers **pregs,
  34. int *nregs)
  35. {
  36. struct device_node *dp;
  37. *nregs = 0;
  38. /*
  39. * Return the PBM's PROM node in case we are it's PCI device,
  40. * as the PBM's reg property is different to standard PCI reg
  41. * properties. We would delete this device entry otherwise,
  42. * which confuses XFree86's device probing...
  43. */
  44. if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
  45. (pdev->vendor == PCI_VENDOR_ID_SUN) &&
  46. (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
  47. pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
  48. pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
  49. pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
  50. pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
  51. return bus_node;
  52. dp = bus_node->child;
  53. while (dp) {
  54. struct linux_prom_pci_registers *regs;
  55. struct property *prop;
  56. int len;
  57. prop = of_find_property(dp, "reg", &len);
  58. if (!prop)
  59. goto do_next_sibling;
  60. regs = prop->value;
  61. if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
  62. *pregs = regs;
  63. *nregs = len / sizeof(struct linux_prom_pci_registers);
  64. return dp;
  65. }
  66. do_next_sibling:
  67. dp = dp->sibling;
  68. }
  69. return NULL;
  70. }
  71. /* Older versions of OBP on PCI systems encode 64-bit MEM
  72. * space assignments incorrectly, this fixes them up. We also
  73. * take the opportunity here to hide other kinds of bogus
  74. * assignments.
  75. */
  76. static void __init fixup_obp_assignments(struct pci_dev *pdev,
  77. struct pcidev_cookie *pcp)
  78. {
  79. int i;
  80. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  81. (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
  82. pdev->device == PCI_DEVICE_ID_AL_M1533)) {
  83. int i;
  84. /* Zap all of the normal resources, they are
  85. * meaningless and generate bogus resource collision
  86. * messages. This is OpenBoot's ill-fated attempt to
  87. * represent the implicit resources that these devices
  88. * have.
  89. */
  90. pcp->num_prom_assignments = 0;
  91. for (i = 0; i < 6; i++) {
  92. pdev->resource[i].start =
  93. pdev->resource[i].end =
  94. pdev->resource[i].flags = 0;
  95. }
  96. pdev->resource[PCI_ROM_RESOURCE].start =
  97. pdev->resource[PCI_ROM_RESOURCE].end =
  98. pdev->resource[PCI_ROM_RESOURCE].flags = 0;
  99. return;
  100. }
  101. for (i = 0; i < pcp->num_prom_assignments; i++) {
  102. struct linux_prom_pci_registers *ap;
  103. int space;
  104. ap = &pcp->prom_assignments[i];
  105. space = ap->phys_hi >> 24;
  106. if ((space & 0x3) == 2 &&
  107. (space & 0x4) != 0) {
  108. ap->phys_hi &= ~(0x7 << 24);
  109. ap->phys_hi |= 0x3 << 24;
  110. }
  111. }
  112. }
  113. /* Fill in the PCI device cookie sysdata for the given
  114. * PCI device. This cookie is the means by which one
  115. * can get to OBP and PCI controller specific information
  116. * for a PCI device.
  117. */
  118. static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
  119. struct pci_dev *pdev,
  120. struct device_node *bus_node)
  121. {
  122. struct linux_prom_pci_registers *pregs = NULL;
  123. struct pcidev_cookie *pcp;
  124. struct device_node *dp;
  125. struct property *prop;
  126. int nregs, len;
  127. dp = find_device_prom_node(pbm, pdev, bus_node,
  128. &pregs, &nregs);
  129. if (!dp) {
  130. /* If it is not in the OBP device tree then
  131. * there must be a damn good reason for it.
  132. *
  133. * So what we do is delete the device from the
  134. * PCI device tree completely. This scenario
  135. * is seen, for example, on CP1500 for the
  136. * second EBUS/HappyMeal pair if the external
  137. * connector for it is not present.
  138. */
  139. pci_remove_bus_device(pdev);
  140. return;
  141. }
  142. pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
  143. if (pcp == NULL) {
  144. prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
  145. prom_halt();
  146. }
  147. pcp->pbm = pbm;
  148. pcp->prom_node = dp;
  149. memcpy(pcp->prom_regs, pregs,
  150. nregs * sizeof(struct linux_prom_pci_registers));
  151. pcp->num_prom_regs = nregs;
  152. /* We can't have the pcidev_cookie assignments be just
  153. * direct pointers into the property value, since they
  154. * are potentially modified by the probing process.
  155. */
  156. prop = of_find_property(dp, "assigned-addresses", &len);
  157. if (!prop) {
  158. pcp->num_prom_assignments = 0;
  159. } else {
  160. memcpy(pcp->prom_assignments, prop->value, len);
  161. pcp->num_prom_assignments =
  162. (len / sizeof(pcp->prom_assignments[0]));
  163. }
  164. if (strcmp(dp->name, "ebus") == 0) {
  165. struct linux_prom_ebus_ranges *erng;
  166. int iter;
  167. /* EBUS is special... */
  168. prop = of_find_property(dp, "ranges", &len);
  169. if (!prop) {
  170. prom_printf("EBUS: Fatal error, no range property\n");
  171. prom_halt();
  172. }
  173. erng = prop->value;
  174. len = (len / sizeof(erng[0]));
  175. for (iter = 0; iter < len; iter++) {
  176. struct linux_prom_ebus_ranges *ep = &erng[iter];
  177. struct linux_prom_pci_registers *ap;
  178. ap = &pcp->prom_assignments[iter];
  179. ap->phys_hi = ep->parent_phys_hi;
  180. ap->phys_mid = ep->parent_phys_mid;
  181. ap->phys_lo = ep->parent_phys_lo;
  182. ap->size_hi = 0;
  183. ap->size_lo = ep->size;
  184. }
  185. pcp->num_prom_assignments = len;
  186. }
  187. fixup_obp_assignments(pdev, pcp);
  188. pdev->sysdata = pcp;
  189. }
  190. void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
  191. struct pci_pbm_info *pbm,
  192. struct device_node *dp)
  193. {
  194. struct pci_dev *pdev, *pdev_next;
  195. struct pci_bus *this_pbus, *pbus_next;
  196. /* This must be _safe because the cookie fillin
  197. routine can delete devices from the tree. */
  198. list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
  199. pdev_cookie_fillin(pbm, pdev, dp);
  200. list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
  201. struct pcidev_cookie *pcp = this_pbus->self->sysdata;
  202. pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
  203. }
  204. }
  205. static void __init bad_assignment(struct pci_dev *pdev,
  206. struct linux_prom_pci_registers *ap,
  207. struct resource *res,
  208. int do_prom_halt)
  209. {
  210. prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
  211. pdev->bus->number, pdev->devfn);
  212. if (ap)
  213. prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
  214. ap->phys_hi, ap->phys_mid, ap->phys_lo,
  215. ap->size_hi, ap->size_lo);
  216. if (res)
  217. prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
  218. res->start, res->end, res->flags);
  219. if (do_prom_halt)
  220. prom_halt();
  221. }
  222. static struct resource *
  223. __init get_root_resource(struct linux_prom_pci_registers *ap,
  224. struct pci_pbm_info *pbm)
  225. {
  226. int space = (ap->phys_hi >> 24) & 3;
  227. switch (space) {
  228. case 0:
  229. /* Configuration space, silently ignore it. */
  230. return NULL;
  231. case 1:
  232. /* 16-bit IO space */
  233. return &pbm->io_space;
  234. case 2:
  235. /* 32-bit MEM space */
  236. return &pbm->mem_space;
  237. case 3:
  238. /* 64-bit MEM space, these are allocated out of
  239. * the 32-bit mem_space range for the PBM, ie.
  240. * we just zero out the upper 32-bits.
  241. */
  242. return &pbm->mem_space;
  243. default:
  244. printk("PCI: What is resource space %x?\n", space);
  245. return NULL;
  246. };
  247. }
  248. static struct resource *
  249. __init get_device_resource(struct linux_prom_pci_registers *ap,
  250. struct pci_dev *pdev)
  251. {
  252. struct resource *res;
  253. int breg = (ap->phys_hi & 0xff);
  254. switch (breg) {
  255. case PCI_ROM_ADDRESS:
  256. /* Unfortunately I have seen several cases where
  257. * buggy FCODE uses a space value of '1' (I/O space)
  258. * in the register property for the ROM address
  259. * so disable this sanity check for now.
  260. */
  261. #if 0
  262. {
  263. int space = (ap->phys_hi >> 24) & 3;
  264. /* It had better be MEM space. */
  265. if (space != 2)
  266. bad_assignment(pdev, ap, NULL, 0);
  267. }
  268. #endif
  269. res = &pdev->resource[PCI_ROM_RESOURCE];
  270. break;
  271. case PCI_BASE_ADDRESS_0:
  272. case PCI_BASE_ADDRESS_1:
  273. case PCI_BASE_ADDRESS_2:
  274. case PCI_BASE_ADDRESS_3:
  275. case PCI_BASE_ADDRESS_4:
  276. case PCI_BASE_ADDRESS_5:
  277. res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
  278. break;
  279. default:
  280. bad_assignment(pdev, ap, NULL, 0);
  281. res = NULL;
  282. break;
  283. };
  284. return res;
  285. }
  286. static int __init pdev_resource_collisions_expected(struct pci_dev *pdev)
  287. {
  288. if (pdev->vendor != PCI_VENDOR_ID_SUN)
  289. return 0;
  290. if (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS ||
  291. pdev->device == PCI_DEVICE_ID_SUN_RIO_1394 ||
  292. pdev->device == PCI_DEVICE_ID_SUN_RIO_USB)
  293. return 1;
  294. return 0;
  295. }
  296. static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
  297. struct pci_dev *pdev)
  298. {
  299. struct pcidev_cookie *pcp = pdev->sysdata;
  300. int i;
  301. for (i = 0; i < pcp->num_prom_assignments; i++) {
  302. struct linux_prom_pci_registers *ap;
  303. struct resource *root, *res;
  304. /* The format of this property is specified in
  305. * the PCI Bus Binding to IEEE1275-1994.
  306. */
  307. ap = &pcp->prom_assignments[i];
  308. root = get_root_resource(ap, pbm);
  309. res = get_device_resource(ap, pdev);
  310. if (root == NULL || res == NULL ||
  311. res->flags == 0)
  312. continue;
  313. /* Ok we know which resource this PROM assignment is
  314. * for, sanity check it.
  315. */
  316. if ((res->start & 0xffffffffUL) != ap->phys_lo)
  317. bad_assignment(pdev, ap, res, 1);
  318. /* If it is a 64-bit MEM space assignment, verify that
  319. * the resource is too and that the upper 32-bits match.
  320. */
  321. if (((ap->phys_hi >> 24) & 3) == 3) {
  322. if (((res->flags & IORESOURCE_MEM) == 0) ||
  323. ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  324. != PCI_BASE_ADDRESS_MEM_TYPE_64))
  325. bad_assignment(pdev, ap, res, 1);
  326. if ((res->start >> 32) != ap->phys_mid)
  327. bad_assignment(pdev, ap, res, 1);
  328. /* PBM cannot generate cpu initiated PIOs
  329. * to the full 64-bit space. Therefore the
  330. * upper 32-bits better be zero. If it is
  331. * not, just skip it and we will assign it
  332. * properly ourselves.
  333. */
  334. if ((res->start >> 32) != 0UL) {
  335. printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
  336. "%016lx for region %ld on device %s\n",
  337. res->start, (res - &pdev->resource[0]), pci_name(pdev));
  338. continue;
  339. }
  340. }
  341. /* Adjust the resource into the physical address space
  342. * of this PBM.
  343. */
  344. pbm->parent->resource_adjust(pdev, res, root);
  345. if (request_resource(root, res) < 0) {
  346. /* OK, there is some conflict. But this is fine
  347. * since we'll reassign it in the fixup pass.
  348. *
  349. * We notify the user that OBP made an error if it
  350. * is a case we don't expect.
  351. */
  352. if (!pdev_resource_collisions_expected(pdev)) {
  353. printk(KERN_ERR "PCI: Address space collision on region %ld "
  354. "[%016lx:%016lx] of device %s\n",
  355. (res - &pdev->resource[0]),
  356. res->start, res->end,
  357. pci_name(pdev));
  358. }
  359. }
  360. }
  361. }
  362. void __init pci_record_assignments(struct pci_pbm_info *pbm,
  363. struct pci_bus *pbus)
  364. {
  365. struct pci_dev *dev;
  366. struct pci_bus *bus;
  367. list_for_each_entry(dev, &pbus->devices, bus_list)
  368. pdev_record_assignments(pbm, dev);
  369. list_for_each_entry(bus, &pbus->children, node)
  370. pci_record_assignments(pbm, bus);
  371. }
  372. /* Return non-zero if PDEV has implicit I/O resources even
  373. * though it may not have an I/O base address register
  374. * active.
  375. */
  376. static int __init has_implicit_io(struct pci_dev *pdev)
  377. {
  378. int class = pdev->class >> 8;
  379. if (class == PCI_CLASS_NOT_DEFINED ||
  380. class == PCI_CLASS_NOT_DEFINED_VGA ||
  381. class == PCI_CLASS_STORAGE_IDE ||
  382. (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  383. return 1;
  384. return 0;
  385. }
  386. static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
  387. struct pci_dev *pdev)
  388. {
  389. u32 reg;
  390. u16 cmd;
  391. int i, io_seen, mem_seen;
  392. io_seen = mem_seen = 0;
  393. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  394. struct resource *root, *res;
  395. unsigned long size, min, max, align;
  396. res = &pdev->resource[i];
  397. if (res->flags & IORESOURCE_IO)
  398. io_seen++;
  399. else if (res->flags & IORESOURCE_MEM)
  400. mem_seen++;
  401. /* If it is already assigned or the resource does
  402. * not exist, there is nothing to do.
  403. */
  404. if (res->parent != NULL || res->flags == 0UL)
  405. continue;
  406. /* Determine the root we allocate from. */
  407. if (res->flags & IORESOURCE_IO) {
  408. root = &pbm->io_space;
  409. min = root->start + 0x400UL;
  410. max = root->end;
  411. } else {
  412. root = &pbm->mem_space;
  413. min = root->start;
  414. max = min + 0x80000000UL;
  415. }
  416. size = res->end - res->start;
  417. align = size + 1;
  418. if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
  419. /* uh oh */
  420. prom_printf("PCI: Failed to allocate resource %d for %s\n",
  421. i, pci_name(pdev));
  422. prom_halt();
  423. }
  424. /* Update PCI config space. */
  425. pbm->parent->base_address_update(pdev, i);
  426. }
  427. /* Special case, disable the ROM. Several devices
  428. * act funny (ie. do not respond to memory space writes)
  429. * when it is left enabled. A good example are Qlogic,ISP
  430. * adapters.
  431. */
  432. pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
  433. reg &= ~PCI_ROM_ADDRESS_ENABLE;
  434. pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
  435. /* If we saw I/O or MEM resources, enable appropriate
  436. * bits in PCI command register.
  437. */
  438. if (io_seen || mem_seen) {
  439. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  440. if (io_seen || has_implicit_io(pdev))
  441. cmd |= PCI_COMMAND_IO;
  442. if (mem_seen)
  443. cmd |= PCI_COMMAND_MEMORY;
  444. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  445. }
  446. /* If this is a PCI bridge or an IDE controller,
  447. * enable bus mastering. In the former case also
  448. * set the cache line size correctly.
  449. */
  450. if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
  451. (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
  452. ((pdev->class & 0x80) != 0))) {
  453. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  454. cmd |= PCI_COMMAND_MASTER;
  455. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  456. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
  457. pci_write_config_byte(pdev,
  458. PCI_CACHE_LINE_SIZE,
  459. (64 / sizeof(u32)));
  460. }
  461. }
  462. void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
  463. struct pci_bus *pbus)
  464. {
  465. struct pci_dev *dev;
  466. struct pci_bus *bus;
  467. list_for_each_entry(dev, &pbus->devices, bus_list)
  468. pdev_assign_unassigned(pbm, dev);
  469. list_for_each_entry(bus, &pbus->children, node)
  470. pci_assign_unassigned(pbm, bus);
  471. }
  472. static inline unsigned int pci_slot_swivel(struct pci_pbm_info *pbm,
  473. struct pci_dev *toplevel_pdev,
  474. struct pci_dev *pdev,
  475. unsigned int interrupt)
  476. {
  477. unsigned int ret;
  478. if (unlikely(interrupt < 1 || interrupt > 4)) {
  479. printk("%s: Device %s interrupt value of %u is strange.\n",
  480. pbm->name, pci_name(pdev), interrupt);
  481. return interrupt;
  482. }
  483. ret = ((interrupt - 1 + (PCI_SLOT(pdev->devfn) & 3)) & 3) + 1;
  484. if (pci_irq_verbose)
  485. printk("%s: %s IRQ Swivel %s [%x:%x] -> [%x]\n",
  486. pbm->name, pci_name(toplevel_pdev), pci_name(pdev),
  487. interrupt, PCI_SLOT(pdev->devfn), ret);
  488. return ret;
  489. }
  490. static inline unsigned int pci_apply_intmap(struct pci_pbm_info *pbm,
  491. struct pci_dev *toplevel_pdev,
  492. struct pci_dev *pbus,
  493. struct pci_dev *pdev,
  494. unsigned int interrupt,
  495. struct device_node **cnode)
  496. {
  497. struct linux_prom_pci_intmap *imap;
  498. struct linux_prom_pci_intmask *imask;
  499. struct pcidev_cookie *pbus_pcp = pbus->sysdata;
  500. struct pcidev_cookie *pdev_pcp = pdev->sysdata;
  501. struct linux_prom_pci_registers *pregs = pdev_pcp->prom_regs;
  502. struct property *prop;
  503. int plen, num_imap, i;
  504. unsigned int hi, mid, lo, irq, orig_interrupt;
  505. *cnode = pbus_pcp->prom_node;
  506. prop = of_find_property(pbus_pcp->prom_node, "interrupt-map", &plen);
  507. if (!prop ||
  508. (plen % sizeof(struct linux_prom_pci_intmap)) != 0) {
  509. printk("%s: Device %s interrupt-map has bad len %d\n",
  510. pbm->name, pci_name(pbus), plen);
  511. goto no_intmap;
  512. }
  513. imap = prop->value;
  514. num_imap = plen / sizeof(struct linux_prom_pci_intmap);
  515. prop = of_find_property(pbus_pcp->prom_node, "interrupt-map-mask", &plen);
  516. if (!prop ||
  517. (plen % sizeof(struct linux_prom_pci_intmask)) != 0) {
  518. printk("%s: Device %s interrupt-map-mask has bad len %d\n",
  519. pbm->name, pci_name(pbus), plen);
  520. goto no_intmap;
  521. }
  522. imask = prop->value;
  523. orig_interrupt = interrupt;
  524. hi = pregs->phys_hi & imask->phys_hi;
  525. mid = pregs->phys_mid & imask->phys_mid;
  526. lo = pregs->phys_lo & imask->phys_lo;
  527. irq = interrupt & imask->interrupt;
  528. for (i = 0; i < num_imap; i++) {
  529. if (imap[i].phys_hi == hi &&
  530. imap[i].phys_mid == mid &&
  531. imap[i].phys_lo == lo &&
  532. imap[i].interrupt == irq) {
  533. *cnode = of_find_node_by_phandle(imap[i].cnode);
  534. interrupt = imap[i].cinterrupt;
  535. }
  536. }
  537. if (pci_irq_verbose)
  538. printk("%s: %s MAP BUS %s DEV %s [%x] -> [%x]\n",
  539. pbm->name, pci_name(toplevel_pdev),
  540. pci_name(pbus), pci_name(pdev),
  541. orig_interrupt, interrupt);
  542. no_intmap:
  543. return interrupt;
  544. }
  545. /* For each PCI bus on the way to the root:
  546. * 1) If it has an interrupt-map property, apply it.
  547. * 2) Else, swivel the interrupt number based upon the PCI device number.
  548. *
  549. * Return the "IRQ controller" node. If this is the PBM's device node,
  550. * all interrupt translations are complete, else we should use that node's
  551. * "reg" property to apply the PBM's "interrupt-{map,mask}" to the interrupt.
  552. */
  553. static struct device_node * __init
  554. pci_intmap_match_to_root(struct pci_pbm_info *pbm,
  555. struct pci_dev *pdev,
  556. unsigned int *interrupt)
  557. {
  558. struct pci_dev *toplevel_pdev = pdev;
  559. struct pcidev_cookie *toplevel_pcp = toplevel_pdev->sysdata;
  560. struct device_node *cnode = toplevel_pcp->prom_node;
  561. while (pdev->bus->number != pbm->pci_first_busno) {
  562. struct pci_dev *pbus = pdev->bus->self;
  563. struct pcidev_cookie *pcp = pbus->sysdata;
  564. struct property *prop;
  565. prop = of_find_property(pcp->prom_node, "interrupt-map", NULL);
  566. if (!prop) {
  567. *interrupt = pci_slot_swivel(pbm, toplevel_pdev,
  568. pdev, *interrupt);
  569. cnode = pcp->prom_node;
  570. } else {
  571. *interrupt = pci_apply_intmap(pbm, toplevel_pdev,
  572. pbus, pdev,
  573. *interrupt, &cnode);
  574. while (pcp->prom_node != cnode &&
  575. pbus->bus->number != pbm->pci_first_busno) {
  576. pbus = pbus->bus->self;
  577. pcp = pbus->sysdata;
  578. }
  579. }
  580. pdev = pbus;
  581. if (cnode == pbm->prom_node)
  582. break;
  583. }
  584. return cnode;
  585. }
  586. static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
  587. {
  588. struct pcidev_cookie *dev_pcp = pdev->sysdata;
  589. struct pci_pbm_info *pbm = dev_pcp->pbm;
  590. struct linux_prom_pci_registers *reg;
  591. struct device_node *cnode;
  592. struct property *prop;
  593. unsigned int hi, mid, lo, irq;
  594. int i, plen;
  595. cnode = pci_intmap_match_to_root(pbm, pdev, interrupt);
  596. if (cnode == pbm->prom_node)
  597. goto success;
  598. prop = of_find_property(cnode, "reg", &plen);
  599. if (!prop ||
  600. (plen % sizeof(struct linux_prom_pci_registers)) != 0) {
  601. printk("%s: OBP node %s reg property has bad len %d\n",
  602. pbm->name, cnode->full_name, plen);
  603. goto fail;
  604. }
  605. reg = prop->value;
  606. hi = reg[0].phys_hi & pbm->pbm_intmask->phys_hi;
  607. mid = reg[0].phys_mid & pbm->pbm_intmask->phys_mid;
  608. lo = reg[0].phys_lo & pbm->pbm_intmask->phys_lo;
  609. irq = *interrupt & pbm->pbm_intmask->interrupt;
  610. for (i = 0; i < pbm->num_pbm_intmap; i++) {
  611. struct linux_prom_pci_intmap *intmap;
  612. intmap = &pbm->pbm_intmap[i];
  613. if (intmap->phys_hi == hi &&
  614. intmap->phys_mid == mid &&
  615. intmap->phys_lo == lo &&
  616. intmap->interrupt == irq) {
  617. *interrupt = intmap->cinterrupt;
  618. goto success;
  619. }
  620. }
  621. fail:
  622. return 0;
  623. success:
  624. if (pci_irq_verbose)
  625. printk("%s: Routing bus[%2x] slot[%2x] to INO[%02x]\n",
  626. pbm->name,
  627. pdev->bus->number, PCI_SLOT(pdev->devfn),
  628. *interrupt);
  629. return 1;
  630. }
  631. static void __init pdev_fixup_irq(struct pci_dev *pdev)
  632. {
  633. struct pcidev_cookie *pcp = pdev->sysdata;
  634. struct pci_pbm_info *pbm = pcp->pbm;
  635. struct pci_controller_info *p = pbm->parent;
  636. unsigned int portid = pbm->portid;
  637. unsigned int prom_irq;
  638. struct device_node *dp = pcp->prom_node;
  639. struct property *prop;
  640. /* If this is an empty EBUS device, sometimes OBP fails to
  641. * give it a valid fully specified interrupts property.
  642. * The EBUS hooked up to SunHME on PCI I/O boards of
  643. * Ex000 systems is one such case.
  644. *
  645. * The interrupt is not important so just ignore it.
  646. */
  647. if (pdev->vendor == PCI_VENDOR_ID_SUN &&
  648. pdev->device == PCI_DEVICE_ID_SUN_EBUS &&
  649. !dp->child) {
  650. pdev->irq = 0;
  651. return;
  652. }
  653. prop = of_find_property(dp, "interrupts", NULL);
  654. if (!prop) {
  655. pdev->irq = 0;
  656. return;
  657. }
  658. prom_irq = *(unsigned int *) prop->value;
  659. if (tlb_type != hypervisor) {
  660. /* Fully specified already? */
  661. if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
  662. pdev->irq = p->irq_build(pbm, pdev, prom_irq);
  663. goto have_irq;
  664. }
  665. /* An onboard device? (bit 5 set) */
  666. if ((prom_irq & PCI_IRQ_INO) & 0x20) {
  667. pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
  668. goto have_irq;
  669. }
  670. }
  671. /* Can we find a matching entry in the interrupt-map? */
  672. if (pci_intmap_match(pdev, &prom_irq)) {
  673. pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
  674. goto have_irq;
  675. }
  676. /* Ok, we have to do it the hard way. */
  677. {
  678. unsigned int bus, slot, line;
  679. bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
  680. /* If we have a legal interrupt property, use it as
  681. * the IRQ line.
  682. */
  683. if (prom_irq > 0 && prom_irq < 5) {
  684. line = ((prom_irq - 1) & 3);
  685. } else {
  686. u8 pci_irq_line;
  687. /* Else just directly consult PCI config space. */
  688. pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
  689. line = ((pci_irq_line - 1) & 3);
  690. }
  691. /* Now figure out the slot.
  692. *
  693. * Basically, device number zero on the top-level bus is
  694. * always the PCI host controller. Slot 0 is then device 1.
  695. * PBM A supports two external slots (0 and 1), and PBM B
  696. * supports 4 external slots (0, 1, 2, and 3). On-board PCI
  697. * devices are wired to device numbers outside of these
  698. * ranges. -DaveM
  699. */
  700. if (pdev->bus->number == pbm->pci_first_busno) {
  701. slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
  702. } else {
  703. struct pci_dev *bus_dev;
  704. /* Underneath a bridge, use slot number of parent
  705. * bridge which is closest to the PBM.
  706. */
  707. bus_dev = pdev->bus->self;
  708. while (bus_dev->bus &&
  709. bus_dev->bus->number != pbm->pci_first_busno)
  710. bus_dev = bus_dev->bus->self;
  711. slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
  712. }
  713. slot = slot << 2;
  714. pdev->irq = p->irq_build(pbm, pdev,
  715. ((portid << 6) & PCI_IRQ_IGN) |
  716. (bus | slot | line));
  717. }
  718. have_irq:
  719. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
  720. pdev->irq & PCI_IRQ_INO);
  721. }
  722. void __init pci_fixup_irq(struct pci_pbm_info *pbm,
  723. struct pci_bus *pbus)
  724. {
  725. struct pci_dev *dev;
  726. struct pci_bus *bus;
  727. list_for_each_entry(dev, &pbus->devices, bus_list)
  728. pdev_fixup_irq(dev);
  729. list_for_each_entry(bus, &pbus->children, node)
  730. pci_fixup_irq(pbm, bus);
  731. }
  732. static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
  733. {
  734. u16 cmd;
  735. u8 hdr_type, min_gnt, ltimer;
  736. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  737. cmd |= PCI_COMMAND_MASTER;
  738. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  739. /* Read it back, if the mastering bit did not
  740. * get set, the device does not support bus
  741. * mastering so we have nothing to do here.
  742. */
  743. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  744. if ((cmd & PCI_COMMAND_MASTER) == 0)
  745. return;
  746. /* Set correct cache line size, 64-byte on all
  747. * Sparc64 PCI systems. Note that the value is
  748. * measured in 32-bit words.
  749. */
  750. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  751. 64 / sizeof(u32));
  752. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
  753. hdr_type &= ~0x80;
  754. if (hdr_type != PCI_HEADER_TYPE_NORMAL)
  755. return;
  756. /* If the latency timer is already programmed with a non-zero
  757. * value, assume whoever set it (OBP or whoever) knows what
  758. * they are doing.
  759. */
  760. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
  761. if (ltimer != 0)
  762. return;
  763. /* XXX Since I'm tipping off the min grant value to
  764. * XXX choose a suitable latency timer value, I also
  765. * XXX considered making use of the max latency value
  766. * XXX as well. Unfortunately I've seen too many bogusly
  767. * XXX low settings for it to the point where it lacks
  768. * XXX any usefulness. In one case, an ethernet card
  769. * XXX claimed a min grant of 10 and a max latency of 5.
  770. * XXX Now, if I had two such cards on the same bus I
  771. * XXX could not set the desired burst period (calculated
  772. * XXX from min grant) without violating the max latency
  773. * XXX bound. Duh...
  774. * XXX
  775. * XXX I blame dumb PC bios implementors for stuff like
  776. * XXX this, most of them don't even try to do something
  777. * XXX sensible with latency timer values and just set some
  778. * XXX default value (usually 32) into every device.
  779. */
  780. pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
  781. if (min_gnt == 0) {
  782. /* If no min_gnt setting then use a default
  783. * value.
  784. */
  785. if (is_66mhz)
  786. ltimer = 16;
  787. else
  788. ltimer = 32;
  789. } else {
  790. int shift_factor;
  791. if (is_66mhz)
  792. shift_factor = 2;
  793. else
  794. shift_factor = 3;
  795. /* Use a default value when the min_gnt value
  796. * is erroneously high.
  797. */
  798. if (((unsigned int) min_gnt << shift_factor) > 512 ||
  799. ((min_gnt << shift_factor) & 0xff) == 0) {
  800. ltimer = 8 << shift_factor;
  801. } else {
  802. ltimer = min_gnt << shift_factor;
  803. }
  804. }
  805. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
  806. }
  807. void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
  808. struct pci_bus *pbus)
  809. {
  810. struct pci_dev *pdev;
  811. int all_are_66mhz;
  812. u16 status;
  813. if (pbm->is_66mhz_capable == 0) {
  814. all_are_66mhz = 0;
  815. goto out;
  816. }
  817. all_are_66mhz = 1;
  818. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  819. pci_read_config_word(pdev, PCI_STATUS, &status);
  820. if (!(status & PCI_STATUS_66MHZ)) {
  821. all_are_66mhz = 0;
  822. break;
  823. }
  824. }
  825. out:
  826. pbm->all_devs_66mhz = all_are_66mhz;
  827. printk("PCI%d(PBM%c): Bus running at %dMHz\n",
  828. pbm->parent->index,
  829. (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
  830. (all_are_66mhz ? 66 : 33));
  831. }
  832. void pci_setup_busmastering(struct pci_pbm_info *pbm,
  833. struct pci_bus *pbus)
  834. {
  835. struct pci_dev *dev;
  836. struct pci_bus *bus;
  837. int is_66mhz;
  838. is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
  839. list_for_each_entry(dev, &pbus->devices, bus_list)
  840. pdev_setup_busmastering(dev, is_66mhz);
  841. list_for_each_entry(bus, &pbus->children, node)
  842. pci_setup_busmastering(pbm, bus);
  843. }
  844. void pci_register_legacy_regions(struct resource *io_res,
  845. struct resource *mem_res)
  846. {
  847. struct resource *p;
  848. /* VGA Video RAM. */
  849. p = kzalloc(sizeof(*p), GFP_KERNEL);
  850. if (!p)
  851. return;
  852. p->name = "Video RAM area";
  853. p->start = mem_res->start + 0xa0000UL;
  854. p->end = p->start + 0x1ffffUL;
  855. p->flags = IORESOURCE_BUSY;
  856. request_resource(mem_res, p);
  857. p = kzalloc(sizeof(*p), GFP_KERNEL);
  858. if (!p)
  859. return;
  860. p->name = "System ROM";
  861. p->start = mem_res->start + 0xf0000UL;
  862. p->end = p->start + 0xffffUL;
  863. p->flags = IORESOURCE_BUSY;
  864. request_resource(mem_res, p);
  865. p = kzalloc(sizeof(*p), GFP_KERNEL);
  866. if (!p)
  867. return;
  868. p->name = "Video ROM";
  869. p->start = mem_res->start + 0xc0000UL;
  870. p->end = p->start + 0x7fffUL;
  871. p->flags = IORESOURCE_BUSY;
  872. request_resource(mem_res, p);
  873. }
  874. /* Generic helper routines for PCI error reporting. */
  875. void pci_scan_for_target_abort(struct pci_controller_info *p,
  876. struct pci_pbm_info *pbm,
  877. struct pci_bus *pbus)
  878. {
  879. struct pci_dev *pdev;
  880. struct pci_bus *bus;
  881. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  882. u16 status, error_bits;
  883. pci_read_config_word(pdev, PCI_STATUS, &status);
  884. error_bits =
  885. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  886. PCI_STATUS_REC_TARGET_ABORT));
  887. if (error_bits) {
  888. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  889. printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
  890. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  891. pci_name(pdev), status);
  892. }
  893. }
  894. list_for_each_entry(bus, &pbus->children, node)
  895. pci_scan_for_target_abort(p, pbm, bus);
  896. }
  897. void pci_scan_for_master_abort(struct pci_controller_info *p,
  898. struct pci_pbm_info *pbm,
  899. struct pci_bus *pbus)
  900. {
  901. struct pci_dev *pdev;
  902. struct pci_bus *bus;
  903. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  904. u16 status, error_bits;
  905. pci_read_config_word(pdev, PCI_STATUS, &status);
  906. error_bits =
  907. (status & (PCI_STATUS_REC_MASTER_ABORT));
  908. if (error_bits) {
  909. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  910. printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
  911. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  912. pci_name(pdev), status);
  913. }
  914. }
  915. list_for_each_entry(bus, &pbus->children, node)
  916. pci_scan_for_master_abort(p, pbm, bus);
  917. }
  918. void pci_scan_for_parity_error(struct pci_controller_info *p,
  919. struct pci_pbm_info *pbm,
  920. struct pci_bus *pbus)
  921. {
  922. struct pci_dev *pdev;
  923. struct pci_bus *bus;
  924. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  925. u16 status, error_bits;
  926. pci_read_config_word(pdev, PCI_STATUS, &status);
  927. error_bits =
  928. (status & (PCI_STATUS_PARITY |
  929. PCI_STATUS_DETECTED_PARITY));
  930. if (error_bits) {
  931. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  932. printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
  933. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  934. pci_name(pdev), status);
  935. }
  936. }
  937. list_for_each_entry(bus, &pbus->children, node)
  938. pci_scan_for_parity_error(p, pbm, bus);
  939. }