pci_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 <asm/of_device.h>
  12. #include "pci_impl.h"
  13. /* Fix self device of BUS and hook it into BUS->self.
  14. * The pci_scan_bus does not do this for the host bridge.
  15. */
  16. void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
  17. {
  18. struct pci_dev *pdev;
  19. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  20. if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
  21. pbus->self = pdev;
  22. return;
  23. }
  24. }
  25. prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
  26. prom_halt();
  27. }
  28. /* Find the OBP PROM device tree node for a PCI device. */
  29. static struct device_node * __init
  30. find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
  31. struct device_node *bus_node,
  32. struct linux_prom_pci_registers **pregs,
  33. int *nregs)
  34. {
  35. struct device_node *dp;
  36. *nregs = 0;
  37. /*
  38. * Return the PBM's PROM node in case we are it's PCI device,
  39. * as the PBM's reg property is different to standard PCI reg
  40. * properties. We would delete this device entry otherwise,
  41. * which confuses XFree86's device probing...
  42. */
  43. if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
  44. (pdev->vendor == PCI_VENDOR_ID_SUN) &&
  45. (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
  46. pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
  47. pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
  48. pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
  49. pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
  50. return bus_node;
  51. dp = bus_node->child;
  52. while (dp) {
  53. struct linux_prom_pci_registers *regs;
  54. struct property *prop;
  55. int len;
  56. prop = of_find_property(dp, "reg", &len);
  57. if (!prop)
  58. goto do_next_sibling;
  59. regs = prop->value;
  60. if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
  61. *pregs = regs;
  62. *nregs = len / sizeof(struct linux_prom_pci_registers);
  63. return dp;
  64. }
  65. do_next_sibling:
  66. dp = dp->sibling;
  67. }
  68. return NULL;
  69. }
  70. /* Older versions of OBP on PCI systems encode 64-bit MEM
  71. * space assignments incorrectly, this fixes them up. We also
  72. * take the opportunity here to hide other kinds of bogus
  73. * assignments.
  74. */
  75. static void __init fixup_obp_assignments(struct pci_dev *pdev,
  76. struct pcidev_cookie *pcp)
  77. {
  78. int i;
  79. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  80. (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
  81. pdev->device == PCI_DEVICE_ID_AL_M1533)) {
  82. int i;
  83. /* Zap all of the normal resources, they are
  84. * meaningless and generate bogus resource collision
  85. * messages. This is OpenBoot's ill-fated attempt to
  86. * represent the implicit resources that these devices
  87. * have.
  88. */
  89. pcp->num_prom_assignments = 0;
  90. for (i = 0; i < 6; i++) {
  91. pdev->resource[i].start =
  92. pdev->resource[i].end =
  93. pdev->resource[i].flags = 0;
  94. }
  95. pdev->resource[PCI_ROM_RESOURCE].start =
  96. pdev->resource[PCI_ROM_RESOURCE].end =
  97. pdev->resource[PCI_ROM_RESOURCE].flags = 0;
  98. return;
  99. }
  100. for (i = 0; i < pcp->num_prom_assignments; i++) {
  101. struct linux_prom_pci_registers *ap;
  102. int space;
  103. ap = &pcp->prom_assignments[i];
  104. space = ap->phys_hi >> 24;
  105. if ((space & 0x3) == 2 &&
  106. (space & 0x4) != 0) {
  107. ap->phys_hi &= ~(0x7 << 24);
  108. ap->phys_hi |= 0x3 << 24;
  109. }
  110. }
  111. }
  112. /* Fill in the PCI device cookie sysdata for the given
  113. * PCI device. This cookie is the means by which one
  114. * can get to OBP and PCI controller specific information
  115. * for a PCI device.
  116. */
  117. static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
  118. struct pci_dev *pdev,
  119. struct device_node *bus_node)
  120. {
  121. struct linux_prom_pci_registers *pregs = NULL;
  122. struct pcidev_cookie *pcp;
  123. struct device_node *dp;
  124. struct property *prop;
  125. int nregs, len;
  126. dp = find_device_prom_node(pbm, pdev, bus_node,
  127. &pregs, &nregs);
  128. if (!dp) {
  129. /* If it is not in the OBP device tree then
  130. * there must be a damn good reason for it.
  131. *
  132. * So what we do is delete the device from the
  133. * PCI device tree completely. This scenario
  134. * is seen, for example, on CP1500 for the
  135. * second EBUS/HappyMeal pair if the external
  136. * connector for it is not present.
  137. */
  138. pci_remove_bus_device(pdev);
  139. return;
  140. }
  141. pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
  142. if (pcp == NULL) {
  143. prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
  144. prom_halt();
  145. }
  146. pcp->pbm = pbm;
  147. pcp->prom_node = dp;
  148. pcp->op = of_find_device_by_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 void __init pdev_fixup_irq(struct pci_dev *pdev)
  473. {
  474. struct pcidev_cookie *pcp = pdev->sysdata;
  475. struct of_device *op = pcp->op;
  476. if (op->irqs[0] == 0xffffffff) {
  477. pdev->irq = PCI_IRQ_NONE;
  478. return;
  479. }
  480. pdev->irq = op->irqs[0];
  481. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
  482. pdev->irq & PCI_IRQ_INO);
  483. }
  484. void __init pci_fixup_irq(struct pci_pbm_info *pbm,
  485. struct pci_bus *pbus)
  486. {
  487. struct pci_dev *dev;
  488. struct pci_bus *bus;
  489. list_for_each_entry(dev, &pbus->devices, bus_list)
  490. pdev_fixup_irq(dev);
  491. list_for_each_entry(bus, &pbus->children, node)
  492. pci_fixup_irq(pbm, bus);
  493. }
  494. static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
  495. {
  496. u16 cmd;
  497. u8 hdr_type, min_gnt, ltimer;
  498. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  499. cmd |= PCI_COMMAND_MASTER;
  500. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  501. /* Read it back, if the mastering bit did not
  502. * get set, the device does not support bus
  503. * mastering so we have nothing to do here.
  504. */
  505. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  506. if ((cmd & PCI_COMMAND_MASTER) == 0)
  507. return;
  508. /* Set correct cache line size, 64-byte on all
  509. * Sparc64 PCI systems. Note that the value is
  510. * measured in 32-bit words.
  511. */
  512. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  513. 64 / sizeof(u32));
  514. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
  515. hdr_type &= ~0x80;
  516. if (hdr_type != PCI_HEADER_TYPE_NORMAL)
  517. return;
  518. /* If the latency timer is already programmed with a non-zero
  519. * value, assume whoever set it (OBP or whoever) knows what
  520. * they are doing.
  521. */
  522. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
  523. if (ltimer != 0)
  524. return;
  525. /* XXX Since I'm tipping off the min grant value to
  526. * XXX choose a suitable latency timer value, I also
  527. * XXX considered making use of the max latency value
  528. * XXX as well. Unfortunately I've seen too many bogusly
  529. * XXX low settings for it to the point where it lacks
  530. * XXX any usefulness. In one case, an ethernet card
  531. * XXX claimed a min grant of 10 and a max latency of 5.
  532. * XXX Now, if I had two such cards on the same bus I
  533. * XXX could not set the desired burst period (calculated
  534. * XXX from min grant) without violating the max latency
  535. * XXX bound. Duh...
  536. * XXX
  537. * XXX I blame dumb PC bios implementors for stuff like
  538. * XXX this, most of them don't even try to do something
  539. * XXX sensible with latency timer values and just set some
  540. * XXX default value (usually 32) into every device.
  541. */
  542. pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
  543. if (min_gnt == 0) {
  544. /* If no min_gnt setting then use a default
  545. * value.
  546. */
  547. if (is_66mhz)
  548. ltimer = 16;
  549. else
  550. ltimer = 32;
  551. } else {
  552. int shift_factor;
  553. if (is_66mhz)
  554. shift_factor = 2;
  555. else
  556. shift_factor = 3;
  557. /* Use a default value when the min_gnt value
  558. * is erroneously high.
  559. */
  560. if (((unsigned int) min_gnt << shift_factor) > 512 ||
  561. ((min_gnt << shift_factor) & 0xff) == 0) {
  562. ltimer = 8 << shift_factor;
  563. } else {
  564. ltimer = min_gnt << shift_factor;
  565. }
  566. }
  567. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
  568. }
  569. void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
  570. struct pci_bus *pbus)
  571. {
  572. struct pci_dev *pdev;
  573. int all_are_66mhz;
  574. u16 status;
  575. if (pbm->is_66mhz_capable == 0) {
  576. all_are_66mhz = 0;
  577. goto out;
  578. }
  579. all_are_66mhz = 1;
  580. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  581. pci_read_config_word(pdev, PCI_STATUS, &status);
  582. if (!(status & PCI_STATUS_66MHZ)) {
  583. all_are_66mhz = 0;
  584. break;
  585. }
  586. }
  587. out:
  588. pbm->all_devs_66mhz = all_are_66mhz;
  589. printk("PCI%d(PBM%c): Bus running at %dMHz\n",
  590. pbm->parent->index,
  591. (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
  592. (all_are_66mhz ? 66 : 33));
  593. }
  594. void pci_setup_busmastering(struct pci_pbm_info *pbm,
  595. struct pci_bus *pbus)
  596. {
  597. struct pci_dev *dev;
  598. struct pci_bus *bus;
  599. int is_66mhz;
  600. is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
  601. list_for_each_entry(dev, &pbus->devices, bus_list)
  602. pdev_setup_busmastering(dev, is_66mhz);
  603. list_for_each_entry(bus, &pbus->children, node)
  604. pci_setup_busmastering(pbm, bus);
  605. }
  606. void pci_register_legacy_regions(struct resource *io_res,
  607. struct resource *mem_res)
  608. {
  609. struct resource *p;
  610. /* VGA Video RAM. */
  611. p = kzalloc(sizeof(*p), GFP_KERNEL);
  612. if (!p)
  613. return;
  614. p->name = "Video RAM area";
  615. p->start = mem_res->start + 0xa0000UL;
  616. p->end = p->start + 0x1ffffUL;
  617. p->flags = IORESOURCE_BUSY;
  618. request_resource(mem_res, p);
  619. p = kzalloc(sizeof(*p), GFP_KERNEL);
  620. if (!p)
  621. return;
  622. p->name = "System ROM";
  623. p->start = mem_res->start + 0xf0000UL;
  624. p->end = p->start + 0xffffUL;
  625. p->flags = IORESOURCE_BUSY;
  626. request_resource(mem_res, p);
  627. p = kzalloc(sizeof(*p), GFP_KERNEL);
  628. if (!p)
  629. return;
  630. p->name = "Video ROM";
  631. p->start = mem_res->start + 0xc0000UL;
  632. p->end = p->start + 0x7fffUL;
  633. p->flags = IORESOURCE_BUSY;
  634. request_resource(mem_res, p);
  635. }
  636. /* Generic helper routines for PCI error reporting. */
  637. void pci_scan_for_target_abort(struct pci_controller_info *p,
  638. struct pci_pbm_info *pbm,
  639. struct pci_bus *pbus)
  640. {
  641. struct pci_dev *pdev;
  642. struct pci_bus *bus;
  643. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  644. u16 status, error_bits;
  645. pci_read_config_word(pdev, PCI_STATUS, &status);
  646. error_bits =
  647. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  648. PCI_STATUS_REC_TARGET_ABORT));
  649. if (error_bits) {
  650. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  651. printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
  652. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  653. pci_name(pdev), status);
  654. }
  655. }
  656. list_for_each_entry(bus, &pbus->children, node)
  657. pci_scan_for_target_abort(p, pbm, bus);
  658. }
  659. void pci_scan_for_master_abort(struct pci_controller_info *p,
  660. struct pci_pbm_info *pbm,
  661. struct pci_bus *pbus)
  662. {
  663. struct pci_dev *pdev;
  664. struct pci_bus *bus;
  665. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  666. u16 status, error_bits;
  667. pci_read_config_word(pdev, PCI_STATUS, &status);
  668. error_bits =
  669. (status & (PCI_STATUS_REC_MASTER_ABORT));
  670. if (error_bits) {
  671. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  672. printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
  673. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  674. pci_name(pdev), status);
  675. }
  676. }
  677. list_for_each_entry(bus, &pbus->children, node)
  678. pci_scan_for_master_abort(p, pbm, bus);
  679. }
  680. void pci_scan_for_parity_error(struct pci_controller_info *p,
  681. struct pci_pbm_info *pbm,
  682. struct pci_bus *pbus)
  683. {
  684. struct pci_dev *pdev;
  685. struct pci_bus *bus;
  686. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  687. u16 status, error_bits;
  688. pci_read_config_word(pdev, PCI_STATUS, &status);
  689. error_bits =
  690. (status & (PCI_STATUS_PARITY |
  691. PCI_STATUS_DETECTED_PARITY));
  692. if (error_bits) {
  693. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  694. printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
  695. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  696. pci_name(pdev), status);
  697. }
  698. }
  699. list_for_each_entry(bus, &pbus->children, node)
  700. pci_scan_for_parity_error(p, pbm, bus);
  701. }