pci.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /*
  2. * Common prep/chrp pci routines. -- Cort
  3. */
  4. #include <linux/config.h>
  5. #include <linux/kernel.h>
  6. #include <linux/pci.h>
  7. #include <linux/delay.h>
  8. #include <linux/string.h>
  9. #include <linux/init.h>
  10. #include <linux/capability.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/bootmem.h>
  14. #include <asm/processor.h>
  15. #include <asm/io.h>
  16. #include <asm/prom.h>
  17. #include <asm/sections.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/irq.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/machdep.h>
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. #define DBG(x...) printk(x)
  26. #else
  27. #define DBG(x...)
  28. #endif
  29. unsigned long isa_io_base = 0;
  30. unsigned long isa_mem_base = 0;
  31. unsigned long pci_dram_offset = 0;
  32. int pcibios_assign_bus_offset = 1;
  33. void pcibios_make_OF_bus_map(void);
  34. static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
  35. static int probe_resource(struct pci_bus *parent, struct resource *pr,
  36. struct resource *res, struct resource **conflict);
  37. static void update_bridge_base(struct pci_bus *bus, int i);
  38. static void pcibios_fixup_resources(struct pci_dev* dev);
  39. static void fixup_broken_pcnet32(struct pci_dev* dev);
  40. static int reparent_resources(struct resource *parent, struct resource *res);
  41. static void fixup_cpc710_pci64(struct pci_dev* dev);
  42. /* By default, we don't re-assign bus numbers.
  43. */
  44. int pci_assign_all_buses;
  45. struct pci_controller* hose_head;
  46. struct pci_controller** hose_tail = &hose_head;
  47. static int pci_bus_count;
  48. static void
  49. fixup_broken_pcnet32(struct pci_dev* dev)
  50. {
  51. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  52. dev->vendor = PCI_VENDOR_ID_AMD;
  53. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  54. }
  55. }
  56. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  57. static void
  58. fixup_cpc710_pci64(struct pci_dev* dev)
  59. {
  60. /* Hide the PCI64 BARs from the kernel as their content doesn't
  61. * fit well in the resource management
  62. */
  63. dev->resource[0].start = dev->resource[0].end = 0;
  64. dev->resource[0].flags = 0;
  65. dev->resource[1].start = dev->resource[1].end = 0;
  66. dev->resource[1].flags = 0;
  67. }
  68. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  69. static void
  70. pcibios_fixup_resources(struct pci_dev *dev)
  71. {
  72. struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
  73. int i;
  74. unsigned long offset;
  75. if (!hose) {
  76. printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
  77. return;
  78. }
  79. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  80. struct resource *res = dev->resource + i;
  81. if (!res->flags)
  82. continue;
  83. if (res->end == 0xffffffff) {
  84. DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
  85. pci_name(dev), i, res->start, res->end);
  86. res->end -= res->start;
  87. res->start = 0;
  88. res->flags |= IORESOURCE_UNSET;
  89. continue;
  90. }
  91. offset = 0;
  92. if (res->flags & IORESOURCE_MEM) {
  93. offset = hose->pci_mem_offset;
  94. } else if (res->flags & IORESOURCE_IO) {
  95. offset = (unsigned long) hose->io_base_virt
  96. - isa_io_base;
  97. }
  98. if (offset != 0) {
  99. res->start += offset;
  100. res->end += offset;
  101. #ifdef DEBUG
  102. printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
  103. i, res->flags, pci_name(dev),
  104. res->start - offset, res->start);
  105. #endif
  106. }
  107. }
  108. /* Call machine specific resource fixup */
  109. if (ppc_md.pcibios_fixup_resources)
  110. ppc_md.pcibios_fixup_resources(dev);
  111. }
  112. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  113. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  114. struct resource *res)
  115. {
  116. unsigned long offset = 0;
  117. struct pci_controller *hose = dev->sysdata;
  118. if (hose && res->flags & IORESOURCE_IO)
  119. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  120. else if (hose && res->flags & IORESOURCE_MEM)
  121. offset = hose->pci_mem_offset;
  122. region->start = res->start - offset;
  123. region->end = res->end - offset;
  124. }
  125. EXPORT_SYMBOL(pcibios_resource_to_bus);
  126. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  127. struct pci_bus_region *region)
  128. {
  129. unsigned long offset = 0;
  130. struct pci_controller *hose = dev->sysdata;
  131. if (hose && res->flags & IORESOURCE_IO)
  132. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  133. else if (hose && res->flags & IORESOURCE_MEM)
  134. offset = hose->pci_mem_offset;
  135. res->start = region->start + offset;
  136. res->end = region->end + offset;
  137. }
  138. EXPORT_SYMBOL(pcibios_bus_to_resource);
  139. /*
  140. * We need to avoid collisions with `mirrored' VGA ports
  141. * and other strange ISA hardware, so we always want the
  142. * addresses to be allocated in the 0x000-0x0ff region
  143. * modulo 0x400.
  144. *
  145. * Why? Because some silly external IO cards only decode
  146. * the low 10 bits of the IO address. The 0x00-0xff region
  147. * is reserved for motherboard devices that decode all 16
  148. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  149. * but we want to try to avoid allocating at 0x2900-0x2bff
  150. * which might have be mirrored at 0x0100-0x03ff..
  151. */
  152. void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  153. unsigned long align)
  154. {
  155. struct pci_dev *dev = data;
  156. if (res->flags & IORESOURCE_IO) {
  157. unsigned long start = res->start;
  158. if (size > 0x100) {
  159. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  160. " (%ld bytes)\n", pci_name(dev),
  161. dev->resource - res, size);
  162. }
  163. if (start & 0x300) {
  164. start = (start + 0x3ff) & ~0x3ff;
  165. res->start = start;
  166. }
  167. }
  168. }
  169. EXPORT_SYMBOL(pcibios_align_resource);
  170. /*
  171. * Handle resources of PCI devices. If the world were perfect, we could
  172. * just allocate all the resource regions and do nothing more. It isn't.
  173. * On the other hand, we cannot just re-allocate all devices, as it would
  174. * require us to know lots of host bridge internals. So we attempt to
  175. * keep as much of the original configuration as possible, but tweak it
  176. * when it's found to be wrong.
  177. *
  178. * Known BIOS problems we have to work around:
  179. * - I/O or memory regions not configured
  180. * - regions configured, but not enabled in the command register
  181. * - bogus I/O addresses above 64K used
  182. * - expansion ROMs left enabled (this may sound harmless, but given
  183. * the fact the PCI specs explicitly allow address decoders to be
  184. * shared between expansion ROMs and other resource regions, it's
  185. * at least dangerous)
  186. *
  187. * Our solution:
  188. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  189. * This gives us fixed barriers on where we can allocate.
  190. * (2) Allocate resources for all enabled devices. If there is
  191. * a collision, just mark the resource as unallocated. Also
  192. * disable expansion ROMs during this step.
  193. * (3) Try to allocate resources for disabled devices. If the
  194. * resources were assigned correctly, everything goes well,
  195. * if they weren't, they won't disturb allocation of other
  196. * resources.
  197. * (4) Assign new addresses to resources which were either
  198. * not configured at all or misconfigured. If explicitly
  199. * requested by the user, configure expansion ROM address
  200. * as well.
  201. */
  202. static void __init
  203. pcibios_allocate_bus_resources(struct list_head *bus_list)
  204. {
  205. struct pci_bus *bus;
  206. int i;
  207. struct resource *res, *pr;
  208. /* Depth-First Search on bus tree */
  209. list_for_each_entry(bus, bus_list, node) {
  210. for (i = 0; i < 4; ++i) {
  211. if ((res = bus->resource[i]) == NULL || !res->flags
  212. || res->start > res->end)
  213. continue;
  214. if (bus->parent == NULL)
  215. pr = (res->flags & IORESOURCE_IO)?
  216. &ioport_resource: &iomem_resource;
  217. else {
  218. pr = pci_find_parent_resource(bus->self, res);
  219. if (pr == res) {
  220. /* this happens when the generic PCI
  221. * code (wrongly) decides that this
  222. * bridge is transparent -- paulus
  223. */
  224. continue;
  225. }
  226. }
  227. DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
  228. res->start, res->end, res->flags, pr);
  229. if (pr) {
  230. if (request_resource(pr, res) == 0)
  231. continue;
  232. /*
  233. * Must be a conflict with an existing entry.
  234. * Move that entry (or entries) under the
  235. * bridge resource and try again.
  236. */
  237. if (reparent_resources(pr, res) == 0)
  238. continue;
  239. }
  240. printk(KERN_ERR "PCI: Cannot allocate resource region "
  241. "%d of PCI bridge %d\n", i, bus->number);
  242. if (pci_relocate_bridge_resource(bus, i))
  243. bus->resource[i] = NULL;
  244. }
  245. pcibios_allocate_bus_resources(&bus->children);
  246. }
  247. }
  248. /*
  249. * Reparent resource children of pr that conflict with res
  250. * under res, and make res replace those children.
  251. */
  252. static int __init
  253. reparent_resources(struct resource *parent, struct resource *res)
  254. {
  255. struct resource *p, **pp;
  256. struct resource **firstpp = NULL;
  257. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  258. if (p->end < res->start)
  259. continue;
  260. if (res->end < p->start)
  261. break;
  262. if (p->start < res->start || p->end > res->end)
  263. return -1; /* not completely contained */
  264. if (firstpp == NULL)
  265. firstpp = pp;
  266. }
  267. if (firstpp == NULL)
  268. return -1; /* didn't find any conflicting entries? */
  269. res->parent = parent;
  270. res->child = *firstpp;
  271. res->sibling = *pp;
  272. *firstpp = res;
  273. *pp = NULL;
  274. for (p = res->child; p != NULL; p = p->sibling) {
  275. p->parent = res;
  276. DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
  277. p->name, p->start, p->end, res->name);
  278. }
  279. return 0;
  280. }
  281. /*
  282. * A bridge has been allocated a range which is outside the range
  283. * of its parent bridge, so it needs to be moved.
  284. */
  285. static int __init
  286. pci_relocate_bridge_resource(struct pci_bus *bus, int i)
  287. {
  288. struct resource *res, *pr, *conflict;
  289. unsigned long try, size;
  290. int j;
  291. struct pci_bus *parent = bus->parent;
  292. if (parent == NULL) {
  293. /* shouldn't ever happen */
  294. printk(KERN_ERR "PCI: can't move host bridge resource\n");
  295. return -1;
  296. }
  297. res = bus->resource[i];
  298. if (res == NULL)
  299. return -1;
  300. pr = NULL;
  301. for (j = 0; j < 4; j++) {
  302. struct resource *r = parent->resource[j];
  303. if (!r)
  304. continue;
  305. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  306. continue;
  307. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
  308. pr = r;
  309. break;
  310. }
  311. if (res->flags & IORESOURCE_PREFETCH)
  312. pr = r;
  313. }
  314. if (pr == NULL)
  315. return -1;
  316. size = res->end - res->start;
  317. if (pr->start > pr->end || size > pr->end - pr->start)
  318. return -1;
  319. try = pr->end;
  320. for (;;) {
  321. res->start = try - size;
  322. res->end = try;
  323. if (probe_resource(bus->parent, pr, res, &conflict) == 0)
  324. break;
  325. if (conflict->start <= pr->start + size)
  326. return -1;
  327. try = conflict->start - 1;
  328. }
  329. if (request_resource(pr, res)) {
  330. DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
  331. res->start, res->end);
  332. return -1; /* "can't happen" */
  333. }
  334. update_bridge_base(bus, i);
  335. printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
  336. bus->number, i, res->start, res->end);
  337. return 0;
  338. }
  339. static int __init
  340. probe_resource(struct pci_bus *parent, struct resource *pr,
  341. struct resource *res, struct resource **conflict)
  342. {
  343. struct pci_bus *bus;
  344. struct pci_dev *dev;
  345. struct resource *r;
  346. int i;
  347. for (r = pr->child; r != NULL; r = r->sibling) {
  348. if (r->end >= res->start && res->end >= r->start) {
  349. *conflict = r;
  350. return 1;
  351. }
  352. }
  353. list_for_each_entry(bus, &parent->children, node) {
  354. for (i = 0; i < 4; ++i) {
  355. if ((r = bus->resource[i]) == NULL)
  356. continue;
  357. if (!r->flags || r->start > r->end || r == res)
  358. continue;
  359. if (pci_find_parent_resource(bus->self, r) != pr)
  360. continue;
  361. if (r->end >= res->start && res->end >= r->start) {
  362. *conflict = r;
  363. return 1;
  364. }
  365. }
  366. }
  367. list_for_each_entry(dev, &parent->devices, bus_list) {
  368. for (i = 0; i < 6; ++i) {
  369. r = &dev->resource[i];
  370. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  371. continue;
  372. if (pci_find_parent_resource(dev, r) != pr)
  373. continue;
  374. if (r->end >= res->start && res->end >= r->start) {
  375. *conflict = r;
  376. return 1;
  377. }
  378. }
  379. }
  380. return 0;
  381. }
  382. static void __init
  383. update_bridge_base(struct pci_bus *bus, int i)
  384. {
  385. struct resource *res = bus->resource[i];
  386. u8 io_base_lo, io_limit_lo;
  387. u16 mem_base, mem_limit;
  388. u16 cmd;
  389. unsigned long start, end, off;
  390. struct pci_dev *dev = bus->self;
  391. struct pci_controller *hose = dev->sysdata;
  392. if (!hose) {
  393. printk("update_bridge_base: no hose?\n");
  394. return;
  395. }
  396. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  397. pci_write_config_word(dev, PCI_COMMAND,
  398. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  399. if (res->flags & IORESOURCE_IO) {
  400. off = (unsigned long) hose->io_base_virt - isa_io_base;
  401. start = res->start - off;
  402. end = res->end - off;
  403. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  404. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  405. if (end > 0xffff) {
  406. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  407. start >> 16);
  408. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  409. end >> 16);
  410. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  411. } else
  412. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  413. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  414. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  415. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  416. == IORESOURCE_MEM) {
  417. off = hose->pci_mem_offset;
  418. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  419. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  420. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  421. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  422. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  423. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  424. off = hose->pci_mem_offset;
  425. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  426. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  427. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  428. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  429. } else {
  430. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  431. pci_name(dev), i, res->flags);
  432. }
  433. pci_write_config_word(dev, PCI_COMMAND, cmd);
  434. }
  435. static inline void alloc_resource(struct pci_dev *dev, int idx)
  436. {
  437. struct resource *pr, *r = &dev->resource[idx];
  438. DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
  439. pci_name(dev), idx, r->start, r->end, r->flags);
  440. pr = pci_find_parent_resource(dev, r);
  441. if (!pr || request_resource(pr, r) < 0) {
  442. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  443. " of device %s\n", idx, pci_name(dev));
  444. if (pr)
  445. DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
  446. pr, pr->start, pr->end, pr->flags);
  447. /* We'll assign a new address later */
  448. r->flags |= IORESOURCE_UNSET;
  449. r->end -= r->start;
  450. r->start = 0;
  451. }
  452. }
  453. static void __init
  454. pcibios_allocate_resources(int pass)
  455. {
  456. struct pci_dev *dev = NULL;
  457. int idx, disabled;
  458. u16 command;
  459. struct resource *r;
  460. for_each_pci_dev(dev) {
  461. pci_read_config_word(dev, PCI_COMMAND, &command);
  462. for (idx = 0; idx < 6; idx++) {
  463. r = &dev->resource[idx];
  464. if (r->parent) /* Already allocated */
  465. continue;
  466. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  467. continue; /* Not assigned at all */
  468. if (r->flags & IORESOURCE_IO)
  469. disabled = !(command & PCI_COMMAND_IO);
  470. else
  471. disabled = !(command & PCI_COMMAND_MEMORY);
  472. if (pass == disabled)
  473. alloc_resource(dev, idx);
  474. }
  475. if (pass)
  476. continue;
  477. r = &dev->resource[PCI_ROM_RESOURCE];
  478. if (r->flags & IORESOURCE_ROM_ENABLE) {
  479. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  480. u32 reg;
  481. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  482. r->flags &= ~IORESOURCE_ROM_ENABLE;
  483. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  484. pci_write_config_dword(dev, dev->rom_base_reg,
  485. reg & ~PCI_ROM_ADDRESS_ENABLE);
  486. }
  487. }
  488. }
  489. static void __init
  490. pcibios_assign_resources(void)
  491. {
  492. struct pci_dev *dev = NULL;
  493. int idx;
  494. struct resource *r;
  495. for_each_pci_dev(dev) {
  496. int class = dev->class >> 8;
  497. /* Don't touch classless devices and host bridges */
  498. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  499. continue;
  500. for (idx = 0; idx < 6; idx++) {
  501. r = &dev->resource[idx];
  502. /*
  503. * We shall assign a new address to this resource,
  504. * either because the BIOS (sic) forgot to do so
  505. * or because we have decided the old address was
  506. * unusable for some reason.
  507. */
  508. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  509. (!ppc_md.pcibios_enable_device_hook ||
  510. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  511. r->flags &= ~IORESOURCE_UNSET;
  512. pci_assign_resource(dev, idx);
  513. }
  514. }
  515. #if 0 /* don't assign ROMs */
  516. r = &dev->resource[PCI_ROM_RESOURCE];
  517. r->end -= r->start;
  518. r->start = 0;
  519. if (r->end)
  520. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  521. #endif
  522. }
  523. }
  524. int
  525. pcibios_enable_resources(struct pci_dev *dev, int mask)
  526. {
  527. u16 cmd, old_cmd;
  528. int idx;
  529. struct resource *r;
  530. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  531. old_cmd = cmd;
  532. for (idx=0; idx<6; idx++) {
  533. /* Only set up the requested stuff */
  534. if (!(mask & (1<<idx)))
  535. continue;
  536. r = &dev->resource[idx];
  537. if (r->flags & IORESOURCE_UNSET) {
  538. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  539. return -EINVAL;
  540. }
  541. if (r->flags & IORESOURCE_IO)
  542. cmd |= PCI_COMMAND_IO;
  543. if (r->flags & IORESOURCE_MEM)
  544. cmd |= PCI_COMMAND_MEMORY;
  545. }
  546. if (dev->resource[PCI_ROM_RESOURCE].start)
  547. cmd |= PCI_COMMAND_MEMORY;
  548. if (cmd != old_cmd) {
  549. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  550. pci_write_config_word(dev, PCI_COMMAND, cmd);
  551. }
  552. return 0;
  553. }
  554. static int next_controller_index;
  555. struct pci_controller * __init
  556. pcibios_alloc_controller(void)
  557. {
  558. struct pci_controller *hose;
  559. hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
  560. memset(hose, 0, sizeof(struct pci_controller));
  561. *hose_tail = hose;
  562. hose_tail = &hose->next;
  563. hose->index = next_controller_index++;
  564. return hose;
  565. }
  566. void pcibios_make_OF_bus_map(void)
  567. {
  568. }
  569. /* Add sysfs properties */
  570. void pcibios_add_platform_entries(struct pci_dev *pdev)
  571. {
  572. }
  573. static int __init
  574. pcibios_init(void)
  575. {
  576. struct pci_controller *hose;
  577. struct pci_bus *bus;
  578. int next_busno;
  579. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  580. /* Scan all of the recorded PCI controllers. */
  581. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  582. if (pci_assign_all_buses)
  583. hose->first_busno = next_busno;
  584. hose->last_busno = 0xff;
  585. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  586. hose->last_busno = bus->subordinate;
  587. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  588. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  589. }
  590. pci_bus_count = next_busno;
  591. /* OpenFirmware based machines need a map of OF bus
  592. * numbers vs. kernel bus numbers since we may have to
  593. * remap them.
  594. */
  595. if (pci_assign_all_buses && have_of)
  596. pcibios_make_OF_bus_map();
  597. /* Do machine dependent PCI interrupt routing */
  598. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  599. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  600. /* Call machine dependent fixup */
  601. if (ppc_md.pcibios_fixup)
  602. ppc_md.pcibios_fixup();
  603. /* Allocate and assign resources */
  604. pcibios_allocate_bus_resources(&pci_root_buses);
  605. pcibios_allocate_resources(0);
  606. pcibios_allocate_resources(1);
  607. pcibios_assign_resources();
  608. /* Call machine dependent post-init code */
  609. if (ppc_md.pcibios_after_init)
  610. ppc_md.pcibios_after_init();
  611. return 0;
  612. }
  613. subsys_initcall(pcibios_init);
  614. unsigned char __init
  615. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  616. {
  617. struct pci_controller *hose = dev->sysdata;
  618. if (dev->bus->number != hose->first_busno) {
  619. u8 pin = *pinp;
  620. do {
  621. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  622. /* Move up the chain of bridges. */
  623. dev = dev->bus->self;
  624. } while (dev->bus->self);
  625. *pinp = pin;
  626. /* The slot is the idsel of the last bridge. */
  627. }
  628. return PCI_SLOT(dev->devfn);
  629. }
  630. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  631. unsigned long start, unsigned long size)
  632. {
  633. return start;
  634. }
  635. void __init pcibios_fixup_bus(struct pci_bus *bus)
  636. {
  637. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  638. unsigned long io_offset;
  639. struct resource *res;
  640. int i;
  641. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  642. if (bus->parent == NULL) {
  643. /* This is a host bridge - fill in its resources */
  644. hose->bus = bus;
  645. bus->resource[0] = res = &hose->io_resource;
  646. if (!res->flags) {
  647. if (io_offset)
  648. printk(KERN_ERR "I/O resource not set for host"
  649. " bridge %d\n", hose->index);
  650. res->start = 0;
  651. res->end = IO_SPACE_LIMIT;
  652. res->flags = IORESOURCE_IO;
  653. }
  654. res->start += io_offset;
  655. res->end += io_offset;
  656. for (i = 0; i < 3; ++i) {
  657. res = &hose->mem_resources[i];
  658. if (!res->flags) {
  659. if (i > 0)
  660. continue;
  661. printk(KERN_ERR "Memory resource not set for "
  662. "host bridge %d\n", hose->index);
  663. res->start = hose->pci_mem_offset;
  664. res->end = ~0U;
  665. res->flags = IORESOURCE_MEM;
  666. }
  667. bus->resource[i+1] = res;
  668. }
  669. } else {
  670. /* This is a subordinate bridge */
  671. pci_read_bridge_bases(bus);
  672. for (i = 0; i < 4; ++i) {
  673. if ((res = bus->resource[i]) == NULL)
  674. continue;
  675. if (!res->flags)
  676. continue;
  677. if (io_offset && (res->flags & IORESOURCE_IO)) {
  678. res->start += io_offset;
  679. res->end += io_offset;
  680. } else if (hose->pci_mem_offset
  681. && (res->flags & IORESOURCE_MEM)) {
  682. res->start += hose->pci_mem_offset;
  683. res->end += hose->pci_mem_offset;
  684. }
  685. }
  686. }
  687. if (ppc_md.pcibios_fixup_bus)
  688. ppc_md.pcibios_fixup_bus(bus);
  689. }
  690. char __init *pcibios_setup(char *str)
  691. {
  692. return str;
  693. }
  694. /* the next one is stolen from the alpha port... */
  695. void __init
  696. pcibios_update_irq(struct pci_dev *dev, int irq)
  697. {
  698. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  699. /* XXX FIXME - update OF device tree node interrupt property */
  700. }
  701. int pcibios_enable_device(struct pci_dev *dev, int mask)
  702. {
  703. u16 cmd, old_cmd;
  704. int idx;
  705. struct resource *r;
  706. if (ppc_md.pcibios_enable_device_hook)
  707. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  708. return -EINVAL;
  709. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  710. old_cmd = cmd;
  711. for (idx=0; idx<6; idx++) {
  712. r = &dev->resource[idx];
  713. if (r->flags & IORESOURCE_UNSET) {
  714. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  715. return -EINVAL;
  716. }
  717. if (r->flags & IORESOURCE_IO)
  718. cmd |= PCI_COMMAND_IO;
  719. if (r->flags & IORESOURCE_MEM)
  720. cmd |= PCI_COMMAND_MEMORY;
  721. }
  722. if (cmd != old_cmd) {
  723. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  724. pci_name(dev), old_cmd, cmd);
  725. pci_write_config_word(dev, PCI_COMMAND, cmd);
  726. }
  727. return 0;
  728. }
  729. struct pci_controller*
  730. pci_bus_to_hose(int bus)
  731. {
  732. struct pci_controller* hose = hose_head;
  733. for (; hose; hose = hose->next)
  734. if (bus >= hose->first_busno && bus <= hose->last_busno)
  735. return hose;
  736. return NULL;
  737. }
  738. void __iomem *
  739. pci_bus_io_base(unsigned int bus)
  740. {
  741. struct pci_controller *hose;
  742. hose = pci_bus_to_hose(bus);
  743. if (!hose)
  744. return NULL;
  745. return hose->io_base_virt;
  746. }
  747. unsigned long
  748. pci_bus_io_base_phys(unsigned int bus)
  749. {
  750. struct pci_controller *hose;
  751. hose = pci_bus_to_hose(bus);
  752. if (!hose)
  753. return 0;
  754. return hose->io_base_phys;
  755. }
  756. unsigned long
  757. pci_bus_mem_base_phys(unsigned int bus)
  758. {
  759. struct pci_controller *hose;
  760. hose = pci_bus_to_hose(bus);
  761. if (!hose)
  762. return 0;
  763. return hose->pci_mem_offset;
  764. }
  765. unsigned long
  766. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  767. {
  768. /* Hack alert again ! See comments in chrp_pci.c
  769. */
  770. struct pci_controller* hose =
  771. (struct pci_controller *)pdev->sysdata;
  772. if (hose && res->flags & IORESOURCE_MEM)
  773. return res->start - hose->pci_mem_offset;
  774. /* We may want to do something with IOs here... */
  775. return res->start;
  776. }
  777. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  778. unsigned long *offset,
  779. enum pci_mmap_state mmap_state)
  780. {
  781. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  782. unsigned long io_offset = 0;
  783. int i, res_bit;
  784. if (hose == 0)
  785. return NULL; /* should never happen */
  786. /* If memory, add on the PCI bridge address offset */
  787. if (mmap_state == pci_mmap_mem) {
  788. *offset += hose->pci_mem_offset;
  789. res_bit = IORESOURCE_MEM;
  790. } else {
  791. io_offset = hose->io_base_virt - ___IO_BASE;
  792. *offset += io_offset;
  793. res_bit = IORESOURCE_IO;
  794. }
  795. /*
  796. * Check that the offset requested corresponds to one of the
  797. * resources of the device.
  798. */
  799. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  800. struct resource *rp = &dev->resource[i];
  801. int flags = rp->flags;
  802. /* treat ROM as memory (should be already) */
  803. if (i == PCI_ROM_RESOURCE)
  804. flags |= IORESOURCE_MEM;
  805. /* Active and same type? */
  806. if ((flags & res_bit) == 0)
  807. continue;
  808. /* In the range of this resource? */
  809. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  810. continue;
  811. /* found it! construct the final physical address */
  812. if (mmap_state == pci_mmap_io)
  813. *offset += hose->io_base_phys - io_offset;
  814. return rp;
  815. }
  816. return NULL;
  817. }
  818. /*
  819. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  820. * device mapping.
  821. */
  822. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  823. pgprot_t protection,
  824. enum pci_mmap_state mmap_state,
  825. int write_combine)
  826. {
  827. unsigned long prot = pgprot_val(protection);
  828. /* Write combine is always 0 on non-memory space mappings. On
  829. * memory space, if the user didn't pass 1, we check for a
  830. * "prefetchable" resource. This is a bit hackish, but we use
  831. * this to workaround the inability of /sysfs to provide a write
  832. * combine bit
  833. */
  834. if (mmap_state != pci_mmap_mem)
  835. write_combine = 0;
  836. else if (write_combine == 0) {
  837. if (rp->flags & IORESOURCE_PREFETCH)
  838. write_combine = 1;
  839. }
  840. /* XXX would be nice to have a way to ask for write-through */
  841. prot |= _PAGE_NO_CACHE;
  842. if (write_combine)
  843. prot &= ~_PAGE_GUARDED;
  844. else
  845. prot |= _PAGE_GUARDED;
  846. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  847. prot);
  848. return __pgprot(prot);
  849. }
  850. /*
  851. * This one is used by /dev/mem and fbdev who have no clue about the
  852. * PCI device, it tries to find the PCI device first and calls the
  853. * above routine
  854. */
  855. pgprot_t pci_phys_mem_access_prot(struct file *file,
  856. unsigned long pfn,
  857. unsigned long size,
  858. pgprot_t protection)
  859. {
  860. struct pci_dev *pdev = NULL;
  861. struct resource *found = NULL;
  862. unsigned long prot = pgprot_val(protection);
  863. unsigned long offset = pfn << PAGE_SHIFT;
  864. int i;
  865. if (page_is_ram(pfn))
  866. return prot;
  867. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  868. for_each_pci_dev(pdev) {
  869. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  870. struct resource *rp = &pdev->resource[i];
  871. int flags = rp->flags;
  872. /* Active and same type? */
  873. if ((flags & IORESOURCE_MEM) == 0)
  874. continue;
  875. /* In the range of this resource? */
  876. if (offset < (rp->start & PAGE_MASK) ||
  877. offset > rp->end)
  878. continue;
  879. found = rp;
  880. break;
  881. }
  882. if (found)
  883. break;
  884. }
  885. if (found) {
  886. if (found->flags & IORESOURCE_PREFETCH)
  887. prot &= ~_PAGE_GUARDED;
  888. pci_dev_put(pdev);
  889. }
  890. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  891. return __pgprot(prot);
  892. }
  893. /*
  894. * Perform the actual remap of the pages for a PCI device mapping, as
  895. * appropriate for this architecture. The region in the process to map
  896. * is described by vm_start and vm_end members of VMA, the base physical
  897. * address is found in vm_pgoff.
  898. * The pci device structure is provided so that architectures may make mapping
  899. * decisions on a per-device or per-bus basis.
  900. *
  901. * Returns a negative error code on failure, zero on success.
  902. */
  903. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  904. enum pci_mmap_state mmap_state,
  905. int write_combine)
  906. {
  907. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  908. struct resource *rp;
  909. int ret;
  910. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  911. if (rp == NULL)
  912. return -EINVAL;
  913. vma->vm_pgoff = offset >> PAGE_SHIFT;
  914. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  915. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  916. vma->vm_page_prot,
  917. mmap_state, write_combine);
  918. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  919. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  920. return ret;
  921. }
  922. /* Obsolete functions. Should be removed once the symbios driver
  923. * is fixed
  924. */
  925. unsigned long
  926. phys_to_bus(unsigned long pa)
  927. {
  928. struct pci_controller *hose;
  929. int i;
  930. for (hose = hose_head; hose; hose = hose->next) {
  931. for (i = 0; i < 3; ++i) {
  932. if (pa >= hose->mem_resources[i].start
  933. && pa <= hose->mem_resources[i].end) {
  934. /*
  935. * XXX the hose->pci_mem_offset really
  936. * only applies to mem_resources[0].
  937. * We need a way to store an offset for
  938. * the others. -- paulus
  939. */
  940. if (i == 0)
  941. pa -= hose->pci_mem_offset;
  942. return pa;
  943. }
  944. }
  945. }
  946. /* hmmm, didn't find it */
  947. return 0;
  948. }
  949. unsigned long
  950. pci_phys_to_bus(unsigned long pa, int busnr)
  951. {
  952. struct pci_controller* hose = pci_bus_to_hose(busnr);
  953. if (!hose)
  954. return pa;
  955. return pa - hose->pci_mem_offset;
  956. }
  957. unsigned long
  958. pci_bus_to_phys(unsigned int ba, int busnr)
  959. {
  960. struct pci_controller* hose = pci_bus_to_hose(busnr);
  961. if (!hose)
  962. return ba;
  963. return ba + hose->pci_mem_offset;
  964. }
  965. /* Provide information on locations of various I/O regions in physical
  966. * memory. Do this on a per-card basis so that we choose the right
  967. * root bridge.
  968. * Note that the returned IO or memory base is a physical address
  969. */
  970. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  971. {
  972. struct pci_controller* hose;
  973. long result = -EOPNOTSUPP;
  974. hose = pci_bus_to_hose(bus);
  975. if (!hose)
  976. return -ENODEV;
  977. switch (which) {
  978. case IOBASE_BRIDGE_NUMBER:
  979. return (long)hose->first_busno;
  980. case IOBASE_MEMORY:
  981. return (long)hose->pci_mem_offset;
  982. case IOBASE_IO:
  983. return (long)hose->io_base_phys;
  984. case IOBASE_ISA_IO:
  985. return (long)isa_io_base;
  986. case IOBASE_ISA_MEM:
  987. return (long)isa_mem_base;
  988. }
  989. return result;
  990. }
  991. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  992. const struct resource *rsrc,
  993. u64 *start, u64 *end)
  994. {
  995. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  996. unsigned long offset = 0;
  997. if (hose == NULL)
  998. return;
  999. if (rsrc->flags & IORESOURCE_IO)
  1000. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1001. *start = rsrc->start + offset;
  1002. *end = rsrc->end + offset;
  1003. }
  1004. void __init
  1005. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1006. int flags, char *name)
  1007. {
  1008. res->start = start;
  1009. res->end = end;
  1010. res->flags = flags;
  1011. res->name = name;
  1012. res->parent = NULL;
  1013. res->sibling = NULL;
  1014. res->child = NULL;
  1015. }
  1016. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1017. {
  1018. unsigned long start = pci_resource_start(dev, bar);
  1019. unsigned long len = pci_resource_len(dev, bar);
  1020. unsigned long flags = pci_resource_flags(dev, bar);
  1021. if (!len)
  1022. return NULL;
  1023. if (max && len > max)
  1024. len = max;
  1025. if (flags & IORESOURCE_IO)
  1026. return ioport_map(start, len);
  1027. if (flags & IORESOURCE_MEM)
  1028. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1029. * not currently distinguish between ioremap and
  1030. * ioremap_nocache.
  1031. */
  1032. return ioremap(start, len);
  1033. /* What? */
  1034. return NULL;
  1035. }
  1036. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1037. {
  1038. /* Nothing to do */
  1039. }
  1040. EXPORT_SYMBOL(pci_iomap);
  1041. EXPORT_SYMBOL(pci_iounmap);
  1042. unsigned long pci_address_to_pio(phys_addr_t address)
  1043. {
  1044. struct pci_controller* hose = hose_head;
  1045. for (; hose; hose = hose->next) {
  1046. unsigned int size = hose->io_resource.end -
  1047. hose->io_resource.start + 1;
  1048. if (address >= hose->io_base_phys &&
  1049. address < (hose->io_base_phys + size)) {
  1050. unsigned long base =
  1051. (unsigned long)hose->io_base_virt - _IO_BASE;
  1052. return base + (address - hose->io_base_phys);
  1053. }
  1054. }
  1055. return (unsigned int)-1;
  1056. }
  1057. EXPORT_SYMBOL(pci_address_to_pio);
  1058. /*
  1059. * Null PCI config access functions, for the case when we can't
  1060. * find a hose.
  1061. */
  1062. #define NULL_PCI_OP(rw, size, type) \
  1063. static int \
  1064. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1065. { \
  1066. return PCIBIOS_DEVICE_NOT_FOUND; \
  1067. }
  1068. static int
  1069. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1070. int len, u32 *val)
  1071. {
  1072. return PCIBIOS_DEVICE_NOT_FOUND;
  1073. }
  1074. static int
  1075. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1076. int len, u32 val)
  1077. {
  1078. return PCIBIOS_DEVICE_NOT_FOUND;
  1079. }
  1080. static struct pci_ops null_pci_ops =
  1081. {
  1082. null_read_config,
  1083. null_write_config
  1084. };
  1085. /*
  1086. * These functions are used early on before PCI scanning is done
  1087. * and all of the pci_dev and pci_bus structures have been created.
  1088. */
  1089. static struct pci_bus *
  1090. fake_pci_bus(struct pci_controller *hose, int busnr)
  1091. {
  1092. static struct pci_bus bus;
  1093. if (hose == 0) {
  1094. hose = pci_bus_to_hose(busnr);
  1095. if (hose == 0)
  1096. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1097. }
  1098. bus.number = busnr;
  1099. bus.sysdata = hose;
  1100. bus.ops = hose? hose->ops: &null_pci_ops;
  1101. return &bus;
  1102. }
  1103. #define EARLY_PCI_OP(rw, size, type) \
  1104. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1105. int devfn, int offset, type value) \
  1106. { \
  1107. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1108. devfn, offset, value); \
  1109. }
  1110. EARLY_PCI_OP(read, byte, u8 *)
  1111. EARLY_PCI_OP(read, word, u16 *)
  1112. EARLY_PCI_OP(read, dword, u32 *)
  1113. EARLY_PCI_OP(write, byte, u8)
  1114. EARLY_PCI_OP(write, word, u16)
  1115. EARLY_PCI_OP(write, dword, u32)