pci.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. #ifdef CONFIG_PPC_OF
  43. static u8* pci_to_OF_bus_map;
  44. #endif
  45. /* By default, we don't re-assign bus numbers.
  46. */
  47. int pci_assign_all_buses;
  48. struct pci_controller* hose_head;
  49. struct pci_controller** hose_tail = &hose_head;
  50. static int pci_bus_count;
  51. static void
  52. fixup_broken_pcnet32(struct pci_dev* dev)
  53. {
  54. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  55. dev->vendor = PCI_VENDOR_ID_AMD;
  56. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  57. }
  58. }
  59. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  60. static void
  61. fixup_cpc710_pci64(struct pci_dev* dev)
  62. {
  63. /* Hide the PCI64 BARs from the kernel as their content doesn't
  64. * fit well in the resource management
  65. */
  66. dev->resource[0].start = dev->resource[0].end = 0;
  67. dev->resource[0].flags = 0;
  68. dev->resource[1].start = dev->resource[1].end = 0;
  69. dev->resource[1].flags = 0;
  70. }
  71. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  72. static void
  73. pcibios_fixup_resources(struct pci_dev *dev)
  74. {
  75. struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
  76. int i;
  77. unsigned long offset;
  78. if (!hose) {
  79. printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
  80. return;
  81. }
  82. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  83. struct resource *res = dev->resource + i;
  84. if (!res->flags)
  85. continue;
  86. if (res->end == 0xffffffff) {
  87. DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
  88. pci_name(dev), i, res->start, res->end);
  89. res->end -= res->start;
  90. res->start = 0;
  91. res->flags |= IORESOURCE_UNSET;
  92. continue;
  93. }
  94. offset = 0;
  95. if (res->flags & IORESOURCE_MEM) {
  96. offset = hose->pci_mem_offset;
  97. } else if (res->flags & IORESOURCE_IO) {
  98. offset = (unsigned long) hose->io_base_virt
  99. - isa_io_base;
  100. }
  101. if (offset != 0) {
  102. res->start += offset;
  103. res->end += offset;
  104. #ifdef DEBUG
  105. printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
  106. i, res->flags, pci_name(dev),
  107. res->start - offset, res->start);
  108. #endif
  109. }
  110. }
  111. /* Call machine specific resource fixup */
  112. if (ppc_md.pcibios_fixup_resources)
  113. ppc_md.pcibios_fixup_resources(dev);
  114. }
  115. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
  116. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  117. struct resource *res)
  118. {
  119. unsigned long offset = 0;
  120. struct pci_controller *hose = dev->sysdata;
  121. if (hose && res->flags & IORESOURCE_IO)
  122. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  123. else if (hose && res->flags & IORESOURCE_MEM)
  124. offset = hose->pci_mem_offset;
  125. region->start = res->start - offset;
  126. region->end = res->end - offset;
  127. }
  128. EXPORT_SYMBOL(pcibios_resource_to_bus);
  129. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  130. struct pci_bus_region *region)
  131. {
  132. unsigned long offset = 0;
  133. struct pci_controller *hose = dev->sysdata;
  134. if (hose && res->flags & IORESOURCE_IO)
  135. offset = (unsigned long)hose->io_base_virt - isa_io_base;
  136. else if (hose && res->flags & IORESOURCE_MEM)
  137. offset = hose->pci_mem_offset;
  138. res->start = region->start + offset;
  139. res->end = region->end + offset;
  140. }
  141. EXPORT_SYMBOL(pcibios_bus_to_resource);
  142. /*
  143. * We need to avoid collisions with `mirrored' VGA ports
  144. * and other strange ISA hardware, so we always want the
  145. * addresses to be allocated in the 0x000-0x0ff region
  146. * modulo 0x400.
  147. *
  148. * Why? Because some silly external IO cards only decode
  149. * the low 10 bits of the IO address. The 0x00-0xff region
  150. * is reserved for motherboard devices that decode all 16
  151. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  152. * but we want to try to avoid allocating at 0x2900-0x2bff
  153. * which might have be mirrored at 0x0100-0x03ff..
  154. */
  155. void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  156. unsigned long align)
  157. {
  158. struct pci_dev *dev = data;
  159. if (res->flags & IORESOURCE_IO) {
  160. unsigned long start = res->start;
  161. if (size > 0x100) {
  162. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  163. " (%ld bytes)\n", pci_name(dev),
  164. dev->resource - res, size);
  165. }
  166. if (start & 0x300) {
  167. start = (start + 0x3ff) & ~0x3ff;
  168. res->start = start;
  169. }
  170. }
  171. }
  172. EXPORT_SYMBOL(pcibios_align_resource);
  173. /*
  174. * Handle resources of PCI devices. If the world were perfect, we could
  175. * just allocate all the resource regions and do nothing more. It isn't.
  176. * On the other hand, we cannot just re-allocate all devices, as it would
  177. * require us to know lots of host bridge internals. So we attempt to
  178. * keep as much of the original configuration as possible, but tweak it
  179. * when it's found to be wrong.
  180. *
  181. * Known BIOS problems we have to work around:
  182. * - I/O or memory regions not configured
  183. * - regions configured, but not enabled in the command register
  184. * - bogus I/O addresses above 64K used
  185. * - expansion ROMs left enabled (this may sound harmless, but given
  186. * the fact the PCI specs explicitly allow address decoders to be
  187. * shared between expansion ROMs and other resource regions, it's
  188. * at least dangerous)
  189. *
  190. * Our solution:
  191. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  192. * This gives us fixed barriers on where we can allocate.
  193. * (2) Allocate resources for all enabled devices. If there is
  194. * a collision, just mark the resource as unallocated. Also
  195. * disable expansion ROMs during this step.
  196. * (3) Try to allocate resources for disabled devices. If the
  197. * resources were assigned correctly, everything goes well,
  198. * if they weren't, they won't disturb allocation of other
  199. * resources.
  200. * (4) Assign new addresses to resources which were either
  201. * not configured at all or misconfigured. If explicitly
  202. * requested by the user, configure expansion ROM address
  203. * as well.
  204. */
  205. static void __init
  206. pcibios_allocate_bus_resources(struct list_head *bus_list)
  207. {
  208. struct pci_bus *bus;
  209. int i;
  210. struct resource *res, *pr;
  211. /* Depth-First Search on bus tree */
  212. list_for_each_entry(bus, bus_list, node) {
  213. for (i = 0; i < 4; ++i) {
  214. if ((res = bus->resource[i]) == NULL || !res->flags
  215. || res->start > res->end)
  216. continue;
  217. if (bus->parent == NULL)
  218. pr = (res->flags & IORESOURCE_IO)?
  219. &ioport_resource: &iomem_resource;
  220. else {
  221. pr = pci_find_parent_resource(bus->self, res);
  222. if (pr == res) {
  223. /* this happens when the generic PCI
  224. * code (wrongly) decides that this
  225. * bridge is transparent -- paulus
  226. */
  227. continue;
  228. }
  229. }
  230. DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
  231. res->start, res->end, res->flags, pr);
  232. if (pr) {
  233. if (request_resource(pr, res) == 0)
  234. continue;
  235. /*
  236. * Must be a conflict with an existing entry.
  237. * Move that entry (or entries) under the
  238. * bridge resource and try again.
  239. */
  240. if (reparent_resources(pr, res) == 0)
  241. continue;
  242. }
  243. printk(KERN_ERR "PCI: Cannot allocate resource region "
  244. "%d of PCI bridge %d\n", i, bus->number);
  245. if (pci_relocate_bridge_resource(bus, i))
  246. bus->resource[i] = NULL;
  247. }
  248. pcibios_allocate_bus_resources(&bus->children);
  249. }
  250. }
  251. /*
  252. * Reparent resource children of pr that conflict with res
  253. * under res, and make res replace those children.
  254. */
  255. static int __init
  256. reparent_resources(struct resource *parent, struct resource *res)
  257. {
  258. struct resource *p, **pp;
  259. struct resource **firstpp = NULL;
  260. for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
  261. if (p->end < res->start)
  262. continue;
  263. if (res->end < p->start)
  264. break;
  265. if (p->start < res->start || p->end > res->end)
  266. return -1; /* not completely contained */
  267. if (firstpp == NULL)
  268. firstpp = pp;
  269. }
  270. if (firstpp == NULL)
  271. return -1; /* didn't find any conflicting entries? */
  272. res->parent = parent;
  273. res->child = *firstpp;
  274. res->sibling = *pp;
  275. *firstpp = res;
  276. *pp = NULL;
  277. for (p = res->child; p != NULL; p = p->sibling) {
  278. p->parent = res;
  279. DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
  280. p->name, p->start, p->end, res->name);
  281. }
  282. return 0;
  283. }
  284. /*
  285. * A bridge has been allocated a range which is outside the range
  286. * of its parent bridge, so it needs to be moved.
  287. */
  288. static int __init
  289. pci_relocate_bridge_resource(struct pci_bus *bus, int i)
  290. {
  291. struct resource *res, *pr, *conflict;
  292. unsigned long try, size;
  293. int j;
  294. struct pci_bus *parent = bus->parent;
  295. if (parent == NULL) {
  296. /* shouldn't ever happen */
  297. printk(KERN_ERR "PCI: can't move host bridge resource\n");
  298. return -1;
  299. }
  300. res = bus->resource[i];
  301. if (res == NULL)
  302. return -1;
  303. pr = NULL;
  304. for (j = 0; j < 4; j++) {
  305. struct resource *r = parent->resource[j];
  306. if (!r)
  307. continue;
  308. if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
  309. continue;
  310. if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
  311. pr = r;
  312. break;
  313. }
  314. if (res->flags & IORESOURCE_PREFETCH)
  315. pr = r;
  316. }
  317. if (pr == NULL)
  318. return -1;
  319. size = res->end - res->start;
  320. if (pr->start > pr->end || size > pr->end - pr->start)
  321. return -1;
  322. try = pr->end;
  323. for (;;) {
  324. res->start = try - size;
  325. res->end = try;
  326. if (probe_resource(bus->parent, pr, res, &conflict) == 0)
  327. break;
  328. if (conflict->start <= pr->start + size)
  329. return -1;
  330. try = conflict->start - 1;
  331. }
  332. if (request_resource(pr, res)) {
  333. DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
  334. res->start, res->end);
  335. return -1; /* "can't happen" */
  336. }
  337. update_bridge_base(bus, i);
  338. printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
  339. bus->number, i, res->start, res->end);
  340. return 0;
  341. }
  342. static int __init
  343. probe_resource(struct pci_bus *parent, struct resource *pr,
  344. struct resource *res, struct resource **conflict)
  345. {
  346. struct pci_bus *bus;
  347. struct pci_dev *dev;
  348. struct resource *r;
  349. int i;
  350. for (r = pr->child; r != NULL; r = r->sibling) {
  351. if (r->end >= res->start && res->end >= r->start) {
  352. *conflict = r;
  353. return 1;
  354. }
  355. }
  356. list_for_each_entry(bus, &parent->children, node) {
  357. for (i = 0; i < 4; ++i) {
  358. if ((r = bus->resource[i]) == NULL)
  359. continue;
  360. if (!r->flags || r->start > r->end || r == res)
  361. continue;
  362. if (pci_find_parent_resource(bus->self, r) != pr)
  363. continue;
  364. if (r->end >= res->start && res->end >= r->start) {
  365. *conflict = r;
  366. return 1;
  367. }
  368. }
  369. }
  370. list_for_each_entry(dev, &parent->devices, bus_list) {
  371. for (i = 0; i < 6; ++i) {
  372. r = &dev->resource[i];
  373. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  374. continue;
  375. if (pci_find_parent_resource(dev, r) != pr)
  376. continue;
  377. if (r->end >= res->start && res->end >= r->start) {
  378. *conflict = r;
  379. return 1;
  380. }
  381. }
  382. }
  383. return 0;
  384. }
  385. static void __init
  386. update_bridge_base(struct pci_bus *bus, int i)
  387. {
  388. struct resource *res = bus->resource[i];
  389. u8 io_base_lo, io_limit_lo;
  390. u16 mem_base, mem_limit;
  391. u16 cmd;
  392. unsigned long start, end, off;
  393. struct pci_dev *dev = bus->self;
  394. struct pci_controller *hose = dev->sysdata;
  395. if (!hose) {
  396. printk("update_bridge_base: no hose?\n");
  397. return;
  398. }
  399. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  400. pci_write_config_word(dev, PCI_COMMAND,
  401. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  402. if (res->flags & IORESOURCE_IO) {
  403. off = (unsigned long) hose->io_base_virt - isa_io_base;
  404. start = res->start - off;
  405. end = res->end - off;
  406. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  407. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  408. if (end > 0xffff) {
  409. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  410. start >> 16);
  411. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  412. end >> 16);
  413. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  414. } else
  415. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  416. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  417. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  418. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  419. == IORESOURCE_MEM) {
  420. off = hose->pci_mem_offset;
  421. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  422. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  423. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  424. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  425. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  426. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  427. off = hose->pci_mem_offset;
  428. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  429. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  430. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  431. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  432. } else {
  433. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  434. pci_name(dev), i, res->flags);
  435. }
  436. pci_write_config_word(dev, PCI_COMMAND, cmd);
  437. }
  438. static inline void alloc_resource(struct pci_dev *dev, int idx)
  439. {
  440. struct resource *pr, *r = &dev->resource[idx];
  441. DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
  442. pci_name(dev), idx, r->start, r->end, r->flags);
  443. pr = pci_find_parent_resource(dev, r);
  444. if (!pr || request_resource(pr, r) < 0) {
  445. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  446. " of device %s\n", idx, pci_name(dev));
  447. if (pr)
  448. DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
  449. pr, pr->start, pr->end, pr->flags);
  450. /* We'll assign a new address later */
  451. r->flags |= IORESOURCE_UNSET;
  452. r->end -= r->start;
  453. r->start = 0;
  454. }
  455. }
  456. static void __init
  457. pcibios_allocate_resources(int pass)
  458. {
  459. struct pci_dev *dev = NULL;
  460. int idx, disabled;
  461. u16 command;
  462. struct resource *r;
  463. for_each_pci_dev(dev) {
  464. pci_read_config_word(dev, PCI_COMMAND, &command);
  465. for (idx = 0; idx < 6; idx++) {
  466. r = &dev->resource[idx];
  467. if (r->parent) /* Already allocated */
  468. continue;
  469. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  470. continue; /* Not assigned at all */
  471. if (r->flags & IORESOURCE_IO)
  472. disabled = !(command & PCI_COMMAND_IO);
  473. else
  474. disabled = !(command & PCI_COMMAND_MEMORY);
  475. if (pass == disabled)
  476. alloc_resource(dev, idx);
  477. }
  478. if (pass)
  479. continue;
  480. r = &dev->resource[PCI_ROM_RESOURCE];
  481. if (r->flags & IORESOURCE_ROM_ENABLE) {
  482. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  483. u32 reg;
  484. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  485. r->flags &= ~IORESOURCE_ROM_ENABLE;
  486. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  487. pci_write_config_dword(dev, dev->rom_base_reg,
  488. reg & ~PCI_ROM_ADDRESS_ENABLE);
  489. }
  490. }
  491. }
  492. static void __init
  493. pcibios_assign_resources(void)
  494. {
  495. struct pci_dev *dev = NULL;
  496. int idx;
  497. struct resource *r;
  498. for_each_pci_dev(dev) {
  499. int class = dev->class >> 8;
  500. /* Don't touch classless devices and host bridges */
  501. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  502. continue;
  503. for (idx = 0; idx < 6; idx++) {
  504. r = &dev->resource[idx];
  505. /*
  506. * We shall assign a new address to this resource,
  507. * either because the BIOS (sic) forgot to do so
  508. * or because we have decided the old address was
  509. * unusable for some reason.
  510. */
  511. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  512. (!ppc_md.pcibios_enable_device_hook ||
  513. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  514. r->flags &= ~IORESOURCE_UNSET;
  515. pci_assign_resource(dev, idx);
  516. }
  517. }
  518. #if 0 /* don't assign ROMs */
  519. r = &dev->resource[PCI_ROM_RESOURCE];
  520. r->end -= r->start;
  521. r->start = 0;
  522. if (r->end)
  523. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  524. #endif
  525. }
  526. }
  527. int
  528. pcibios_enable_resources(struct pci_dev *dev, int mask)
  529. {
  530. u16 cmd, old_cmd;
  531. int idx;
  532. struct resource *r;
  533. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  534. old_cmd = cmd;
  535. for (idx=0; idx<6; idx++) {
  536. /* Only set up the requested stuff */
  537. if (!(mask & (1<<idx)))
  538. continue;
  539. r = &dev->resource[idx];
  540. if (r->flags & IORESOURCE_UNSET) {
  541. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  542. return -EINVAL;
  543. }
  544. if (r->flags & IORESOURCE_IO)
  545. cmd |= PCI_COMMAND_IO;
  546. if (r->flags & IORESOURCE_MEM)
  547. cmd |= PCI_COMMAND_MEMORY;
  548. }
  549. if (dev->resource[PCI_ROM_RESOURCE].start)
  550. cmd |= PCI_COMMAND_MEMORY;
  551. if (cmd != old_cmd) {
  552. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  553. pci_write_config_word(dev, PCI_COMMAND, cmd);
  554. }
  555. return 0;
  556. }
  557. static int next_controller_index;
  558. struct pci_controller * __init
  559. pcibios_alloc_controller(void)
  560. {
  561. struct pci_controller *hose;
  562. hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
  563. memset(hose, 0, sizeof(struct pci_controller));
  564. *hose_tail = hose;
  565. hose_tail = &hose->next;
  566. hose->index = next_controller_index++;
  567. return hose;
  568. }
  569. #ifdef CONFIG_PPC_OF
  570. /*
  571. * Functions below are used on OpenFirmware machines.
  572. */
  573. static void
  574. make_one_node_map(struct device_node* node, u8 pci_bus)
  575. {
  576. int *bus_range;
  577. int len;
  578. if (pci_bus >= pci_bus_count)
  579. return;
  580. bus_range = (int *) get_property(node, "bus-range", &len);
  581. if (bus_range == NULL || len < 2 * sizeof(int)) {
  582. printk(KERN_WARNING "Can't get bus-range for %s, "
  583. "assuming it starts at 0\n", node->full_name);
  584. pci_to_OF_bus_map[pci_bus] = 0;
  585. } else
  586. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  587. for (node=node->child; node != 0;node = node->sibling) {
  588. struct pci_dev* dev;
  589. unsigned int *class_code, *reg;
  590. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  591. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  592. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  593. continue;
  594. reg = (unsigned int *)get_property(node, "reg", NULL);
  595. if (!reg)
  596. continue;
  597. dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  598. if (!dev || !dev->subordinate)
  599. continue;
  600. make_one_node_map(node, dev->subordinate->number);
  601. }
  602. }
  603. void
  604. pcibios_make_OF_bus_map(void)
  605. {
  606. int i;
  607. struct pci_controller* hose;
  608. u8* of_prop_map;
  609. pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
  610. if (!pci_to_OF_bus_map) {
  611. printk(KERN_ERR "Can't allocate OF bus map !\n");
  612. return;
  613. }
  614. /* We fill the bus map with invalid values, that helps
  615. * debugging.
  616. */
  617. for (i=0; i<pci_bus_count; i++)
  618. pci_to_OF_bus_map[i] = 0xff;
  619. /* For each hose, we begin searching bridges */
  620. for(hose=hose_head; hose; hose=hose->next) {
  621. struct device_node* node;
  622. node = (struct device_node *)hose->arch_data;
  623. if (!node)
  624. continue;
  625. make_one_node_map(node, hose->first_busno);
  626. }
  627. of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
  628. if (of_prop_map)
  629. memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
  630. #ifdef DEBUG
  631. printk("PCI->OF bus map:\n");
  632. for (i=0; i<pci_bus_count; i++) {
  633. if (pci_to_OF_bus_map[i] == 0xff)
  634. continue;
  635. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  636. }
  637. #endif
  638. }
  639. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  640. static struct device_node*
  641. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  642. {
  643. struct device_node* sub_node;
  644. for (; node != 0;node = node->sibling) {
  645. unsigned int *class_code;
  646. if (filter(node, data))
  647. return node;
  648. /* For PCI<->PCI bridges or CardBus bridges, we go down
  649. * Note: some OFs create a parent node "multifunc-device" as
  650. * a fake root for all functions of a multi-function device,
  651. * we go down them as well.
  652. */
  653. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  654. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  655. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  656. strcmp(node->name, "multifunc-device"))
  657. continue;
  658. sub_node = scan_OF_pci_childs(node->child, filter, data);
  659. if (sub_node)
  660. return sub_node;
  661. }
  662. return NULL;
  663. }
  664. static int
  665. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  666. {
  667. unsigned int *reg;
  668. u8* fdata = (u8*)data;
  669. reg = (unsigned int *) get_property(node, "reg", NULL);
  670. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  671. && ((reg[0] >> 16) & 0xff) == fdata[0])
  672. return 1;
  673. return 0;
  674. }
  675. static struct device_node*
  676. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  677. {
  678. u8 filter_data[2] = {bus, dev_fn};
  679. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  680. }
  681. /*
  682. * Scans the OF tree for a device node matching a PCI device
  683. */
  684. struct device_node *
  685. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  686. {
  687. struct pci_controller *hose;
  688. struct device_node *node;
  689. int busnr;
  690. if (!have_of)
  691. return NULL;
  692. /* Lookup the hose */
  693. busnr = bus->number;
  694. hose = pci_bus_to_hose(busnr);
  695. if (!hose)
  696. return NULL;
  697. /* Check it has an OF node associated */
  698. node = (struct device_node *) hose->arch_data;
  699. if (!node)
  700. return NULL;
  701. /* Fixup bus number according to what OF think it is. */
  702. if (pci_to_OF_bus_map)
  703. busnr = pci_to_OF_bus_map[busnr];
  704. if (busnr == 0xff)
  705. return NULL;
  706. /* Now, lookup childs of the hose */
  707. return scan_OF_childs_for_device(node->child, busnr, devfn);
  708. }
  709. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  710. struct device_node*
  711. pci_device_to_OF_node(struct pci_dev *dev)
  712. {
  713. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  714. }
  715. EXPORT_SYMBOL(pci_device_to_OF_node);
  716. /* This routine is meant to be used early during boot, when the
  717. * PCI bus numbers have not yet been assigned, and you need to
  718. * issue PCI config cycles to an OF device.
  719. * It could also be used to "fix" RTAS config cycles if you want
  720. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  721. * config cycles.
  722. */
  723. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  724. {
  725. if (!have_of)
  726. return NULL;
  727. while(node) {
  728. struct pci_controller* hose;
  729. for (hose=hose_head;hose;hose=hose->next)
  730. if (hose->arch_data == node)
  731. return hose;
  732. node=node->parent;
  733. }
  734. return NULL;
  735. }
  736. static int
  737. find_OF_pci_device_filter(struct device_node* node, void* data)
  738. {
  739. return ((void *)node == data);
  740. }
  741. /*
  742. * Returns the PCI device matching a given OF node
  743. */
  744. int
  745. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  746. {
  747. unsigned int *reg;
  748. struct pci_controller* hose;
  749. struct pci_dev* dev = NULL;
  750. if (!have_of)
  751. return -ENODEV;
  752. /* Make sure it's really a PCI device */
  753. hose = pci_find_hose_for_OF_device(node);
  754. if (!hose || !hose->arch_data)
  755. return -ENODEV;
  756. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  757. find_OF_pci_device_filter, (void *)node))
  758. return -ENODEV;
  759. reg = (unsigned int *) get_property(node, "reg", NULL);
  760. if (!reg)
  761. return -ENODEV;
  762. *bus = (reg[0] >> 16) & 0xff;
  763. *devfn = ((reg[0] >> 8) & 0xff);
  764. /* Ok, here we need some tweak. If we have already renumbered
  765. * all busses, we can't rely on the OF bus number any more.
  766. * the pci_to_OF_bus_map is not enough as several PCI busses
  767. * may match the same OF bus number.
  768. */
  769. if (!pci_to_OF_bus_map)
  770. return 0;
  771. for_each_pci_dev(dev)
  772. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  773. dev->devfn == *devfn) {
  774. *bus = dev->bus->number;
  775. pci_dev_put(dev);
  776. return 0;
  777. }
  778. return -ENODEV;
  779. }
  780. EXPORT_SYMBOL(pci_device_from_OF_node);
  781. void __init
  782. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  783. struct device_node *dev, int primary)
  784. {
  785. static unsigned int static_lc_ranges[256] __initdata;
  786. unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
  787. unsigned int size;
  788. int rlen = 0, orig_rlen;
  789. int memno = 0;
  790. struct resource *res;
  791. int np, na = prom_n_addr_cells(dev);
  792. np = na + 5;
  793. /* First we try to merge ranges to fix a problem with some pmacs
  794. * that can have more than 3 ranges, fortunately using contiguous
  795. * addresses -- BenH
  796. */
  797. dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  798. if (!dt_ranges)
  799. return;
  800. /* Sanity check, though hopefully that never happens */
  801. if (rlen > sizeof(static_lc_ranges)) {
  802. printk(KERN_WARNING "OF ranges property too large !\n");
  803. rlen = sizeof(static_lc_ranges);
  804. }
  805. lc_ranges = static_lc_ranges;
  806. memcpy(lc_ranges, dt_ranges, rlen);
  807. orig_rlen = rlen;
  808. /* Let's work on a copy of the "ranges" property instead of damaging
  809. * the device-tree image in memory
  810. */
  811. ranges = lc_ranges;
  812. prev = NULL;
  813. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  814. if (prev) {
  815. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  816. (prev[2] + prev[na+4]) == ranges[2] &&
  817. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  818. prev[na+4] += ranges[na+4];
  819. ranges[0] = 0;
  820. ranges += np;
  821. continue;
  822. }
  823. }
  824. prev = ranges;
  825. ranges += np;
  826. }
  827. /*
  828. * The ranges property is laid out as an array of elements,
  829. * each of which comprises:
  830. * cells 0 - 2: a PCI address
  831. * cells 3 or 3+4: a CPU physical address
  832. * (size depending on dev->n_addr_cells)
  833. * cells 4+5 or 5+6: the size of the range
  834. */
  835. ranges = lc_ranges;
  836. rlen = orig_rlen;
  837. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  838. res = NULL;
  839. size = ranges[na+4];
  840. switch ((ranges[0] >> 24) & 0x3) {
  841. case 1: /* I/O space */
  842. if (ranges[2] != 0)
  843. break;
  844. hose->io_base_phys = ranges[na+2];
  845. /* limit I/O space to 16MB */
  846. if (size > 0x01000000)
  847. size = 0x01000000;
  848. hose->io_base_virt = ioremap(ranges[na+2], size);
  849. if (primary)
  850. isa_io_base = (unsigned long) hose->io_base_virt;
  851. res = &hose->io_resource;
  852. res->flags = IORESOURCE_IO;
  853. res->start = ranges[2];
  854. DBG("PCI: IO 0x%lx -> 0x%lx\n",
  855. res->start, res->start + size - 1);
  856. break;
  857. case 2: /* memory space */
  858. memno = 0;
  859. if (ranges[1] == 0 && ranges[2] == 0
  860. && ranges[na+4] <= (16 << 20)) {
  861. /* 1st 16MB, i.e. ISA memory area */
  862. if (primary)
  863. isa_mem_base = ranges[na+2];
  864. memno = 1;
  865. }
  866. while (memno < 3 && hose->mem_resources[memno].flags)
  867. ++memno;
  868. if (memno == 0)
  869. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  870. if (memno < 3) {
  871. res = &hose->mem_resources[memno];
  872. res->flags = IORESOURCE_MEM;
  873. if(ranges[0] & 0x40000000)
  874. res->flags |= IORESOURCE_PREFETCH;
  875. res->start = ranges[na+2];
  876. DBG("PCI: MEM[%d] 0x%lx -> 0x%lx\n", memno,
  877. res->start, res->start + size - 1);
  878. }
  879. break;
  880. }
  881. if (res != NULL) {
  882. res->name = dev->full_name;
  883. res->end = res->start + size - 1;
  884. res->parent = NULL;
  885. res->sibling = NULL;
  886. res->child = NULL;
  887. }
  888. ranges += np;
  889. }
  890. }
  891. /* We create the "pci-OF-bus-map" property now so it appears in the
  892. * /proc device tree
  893. */
  894. void __init
  895. pci_create_OF_bus_map(void)
  896. {
  897. struct property* of_prop;
  898. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  899. if (of_prop && find_path_device("/")) {
  900. memset(of_prop, -1, sizeof(struct property) + 256);
  901. of_prop->name = "pci-OF-bus-map";
  902. of_prop->length = 256;
  903. of_prop->value = (unsigned char *)&of_prop[1];
  904. prom_add_property(find_path_device("/"), of_prop);
  905. }
  906. }
  907. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  908. {
  909. struct pci_dev *pdev;
  910. struct device_node *np;
  911. pdev = to_pci_dev (dev);
  912. np = pci_device_to_OF_node(pdev);
  913. if (np == NULL || np->full_name == NULL)
  914. return 0;
  915. return sprintf(buf, "%s", np->full_name);
  916. }
  917. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  918. #else /* CONFIG_PPC_OF */
  919. void pcibios_make_OF_bus_map(void)
  920. {
  921. }
  922. #endif /* CONFIG_PPC_OF */
  923. /* Add sysfs properties */
  924. void pcibios_add_platform_entries(struct pci_dev *pdev)
  925. {
  926. #ifdef CONFIG_PPC_OF
  927. device_create_file(&pdev->dev, &dev_attr_devspec);
  928. #endif /* CONFIG_PPC_OF */
  929. }
  930. static int __init
  931. pcibios_init(void)
  932. {
  933. struct pci_controller *hose;
  934. struct pci_bus *bus;
  935. int next_busno;
  936. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  937. /* Scan all of the recorded PCI controllers. */
  938. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  939. if (pci_assign_all_buses)
  940. hose->first_busno = next_busno;
  941. hose->last_busno = 0xff;
  942. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  943. hose->last_busno = bus->subordinate;
  944. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  945. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  946. }
  947. pci_bus_count = next_busno;
  948. /* OpenFirmware based machines need a map of OF bus
  949. * numbers vs. kernel bus numbers since we may have to
  950. * remap them.
  951. */
  952. if (pci_assign_all_buses && have_of)
  953. pcibios_make_OF_bus_map();
  954. /* Do machine dependent PCI interrupt routing */
  955. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  956. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  957. /* Call machine dependent fixup */
  958. if (ppc_md.pcibios_fixup)
  959. ppc_md.pcibios_fixup();
  960. /* Allocate and assign resources */
  961. pcibios_allocate_bus_resources(&pci_root_buses);
  962. pcibios_allocate_resources(0);
  963. pcibios_allocate_resources(1);
  964. pcibios_assign_resources();
  965. /* Call machine dependent post-init code */
  966. if (ppc_md.pcibios_after_init)
  967. ppc_md.pcibios_after_init();
  968. return 0;
  969. }
  970. subsys_initcall(pcibios_init);
  971. unsigned char __init
  972. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  973. {
  974. struct pci_controller *hose = dev->sysdata;
  975. if (dev->bus->number != hose->first_busno) {
  976. u8 pin = *pinp;
  977. do {
  978. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  979. /* Move up the chain of bridges. */
  980. dev = dev->bus->self;
  981. } while (dev->bus->self);
  982. *pinp = pin;
  983. /* The slot is the idsel of the last bridge. */
  984. }
  985. return PCI_SLOT(dev->devfn);
  986. }
  987. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  988. unsigned long start, unsigned long size)
  989. {
  990. return start;
  991. }
  992. void __init pcibios_fixup_bus(struct pci_bus *bus)
  993. {
  994. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  995. unsigned long io_offset;
  996. struct resource *res;
  997. int i;
  998. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  999. if (bus->parent == NULL) {
  1000. /* This is a host bridge - fill in its resources */
  1001. hose->bus = bus;
  1002. bus->resource[0] = res = &hose->io_resource;
  1003. if (!res->flags) {
  1004. if (io_offset)
  1005. printk(KERN_ERR "I/O resource not set for host"
  1006. " bridge %d\n", hose->index);
  1007. res->start = 0;
  1008. res->end = IO_SPACE_LIMIT;
  1009. res->flags = IORESOURCE_IO;
  1010. }
  1011. res->start += io_offset;
  1012. res->end += io_offset;
  1013. for (i = 0; i < 3; ++i) {
  1014. res = &hose->mem_resources[i];
  1015. if (!res->flags) {
  1016. if (i > 0)
  1017. continue;
  1018. printk(KERN_ERR "Memory resource not set for "
  1019. "host bridge %d\n", hose->index);
  1020. res->start = hose->pci_mem_offset;
  1021. res->end = ~0U;
  1022. res->flags = IORESOURCE_MEM;
  1023. }
  1024. bus->resource[i+1] = res;
  1025. }
  1026. } else {
  1027. /* This is a subordinate bridge */
  1028. pci_read_bridge_bases(bus);
  1029. for (i = 0; i < 4; ++i) {
  1030. if ((res = bus->resource[i]) == NULL)
  1031. continue;
  1032. if (!res->flags)
  1033. continue;
  1034. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1035. res->start += io_offset;
  1036. res->end += io_offset;
  1037. } else if (hose->pci_mem_offset
  1038. && (res->flags & IORESOURCE_MEM)) {
  1039. res->start += hose->pci_mem_offset;
  1040. res->end += hose->pci_mem_offset;
  1041. }
  1042. }
  1043. }
  1044. if (ppc_md.pcibios_fixup_bus)
  1045. ppc_md.pcibios_fixup_bus(bus);
  1046. }
  1047. char __init *pcibios_setup(char *str)
  1048. {
  1049. return str;
  1050. }
  1051. /* the next one is stolen from the alpha port... */
  1052. void __init
  1053. pcibios_update_irq(struct pci_dev *dev, int irq)
  1054. {
  1055. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1056. /* XXX FIXME - update OF device tree node interrupt property */
  1057. }
  1058. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1059. {
  1060. u16 cmd, old_cmd;
  1061. int idx;
  1062. struct resource *r;
  1063. if (ppc_md.pcibios_enable_device_hook)
  1064. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1065. return -EINVAL;
  1066. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1067. old_cmd = cmd;
  1068. for (idx=0; idx<6; idx++) {
  1069. r = &dev->resource[idx];
  1070. if (r->flags & IORESOURCE_UNSET) {
  1071. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1072. return -EINVAL;
  1073. }
  1074. if (r->flags & IORESOURCE_IO)
  1075. cmd |= PCI_COMMAND_IO;
  1076. if (r->flags & IORESOURCE_MEM)
  1077. cmd |= PCI_COMMAND_MEMORY;
  1078. }
  1079. if (cmd != old_cmd) {
  1080. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1081. pci_name(dev), old_cmd, cmd);
  1082. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1083. }
  1084. return 0;
  1085. }
  1086. struct pci_controller*
  1087. pci_bus_to_hose(int bus)
  1088. {
  1089. struct pci_controller* hose = hose_head;
  1090. for (; hose; hose = hose->next)
  1091. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1092. return hose;
  1093. return NULL;
  1094. }
  1095. void __iomem *
  1096. pci_bus_io_base(unsigned int bus)
  1097. {
  1098. struct pci_controller *hose;
  1099. hose = pci_bus_to_hose(bus);
  1100. if (!hose)
  1101. return NULL;
  1102. return hose->io_base_virt;
  1103. }
  1104. unsigned long
  1105. pci_bus_io_base_phys(unsigned int bus)
  1106. {
  1107. struct pci_controller *hose;
  1108. hose = pci_bus_to_hose(bus);
  1109. if (!hose)
  1110. return 0;
  1111. return hose->io_base_phys;
  1112. }
  1113. unsigned long
  1114. pci_bus_mem_base_phys(unsigned int bus)
  1115. {
  1116. struct pci_controller *hose;
  1117. hose = pci_bus_to_hose(bus);
  1118. if (!hose)
  1119. return 0;
  1120. return hose->pci_mem_offset;
  1121. }
  1122. unsigned long
  1123. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1124. {
  1125. /* Hack alert again ! See comments in chrp_pci.c
  1126. */
  1127. struct pci_controller* hose =
  1128. (struct pci_controller *)pdev->sysdata;
  1129. if (hose && res->flags & IORESOURCE_MEM)
  1130. return res->start - hose->pci_mem_offset;
  1131. /* We may want to do something with IOs here... */
  1132. return res->start;
  1133. }
  1134. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1135. unsigned long *offset,
  1136. enum pci_mmap_state mmap_state)
  1137. {
  1138. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1139. unsigned long io_offset = 0;
  1140. int i, res_bit;
  1141. if (hose == 0)
  1142. return NULL; /* should never happen */
  1143. /* If memory, add on the PCI bridge address offset */
  1144. if (mmap_state == pci_mmap_mem) {
  1145. *offset += hose->pci_mem_offset;
  1146. res_bit = IORESOURCE_MEM;
  1147. } else {
  1148. io_offset = hose->io_base_virt - ___IO_BASE;
  1149. *offset += io_offset;
  1150. res_bit = IORESOURCE_IO;
  1151. }
  1152. /*
  1153. * Check that the offset requested corresponds to one of the
  1154. * resources of the device.
  1155. */
  1156. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1157. struct resource *rp = &dev->resource[i];
  1158. int flags = rp->flags;
  1159. /* treat ROM as memory (should be already) */
  1160. if (i == PCI_ROM_RESOURCE)
  1161. flags |= IORESOURCE_MEM;
  1162. /* Active and same type? */
  1163. if ((flags & res_bit) == 0)
  1164. continue;
  1165. /* In the range of this resource? */
  1166. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1167. continue;
  1168. /* found it! construct the final physical address */
  1169. if (mmap_state == pci_mmap_io)
  1170. *offset += hose->io_base_phys - io_offset;
  1171. return rp;
  1172. }
  1173. return NULL;
  1174. }
  1175. /*
  1176. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1177. * device mapping.
  1178. */
  1179. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1180. pgprot_t protection,
  1181. enum pci_mmap_state mmap_state,
  1182. int write_combine)
  1183. {
  1184. unsigned long prot = pgprot_val(protection);
  1185. /* Write combine is always 0 on non-memory space mappings. On
  1186. * memory space, if the user didn't pass 1, we check for a
  1187. * "prefetchable" resource. This is a bit hackish, but we use
  1188. * this to workaround the inability of /sysfs to provide a write
  1189. * combine bit
  1190. */
  1191. if (mmap_state != pci_mmap_mem)
  1192. write_combine = 0;
  1193. else if (write_combine == 0) {
  1194. if (rp->flags & IORESOURCE_PREFETCH)
  1195. write_combine = 1;
  1196. }
  1197. /* XXX would be nice to have a way to ask for write-through */
  1198. prot |= _PAGE_NO_CACHE;
  1199. if (write_combine)
  1200. prot &= ~_PAGE_GUARDED;
  1201. else
  1202. prot |= _PAGE_GUARDED;
  1203. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  1204. prot);
  1205. return __pgprot(prot);
  1206. }
  1207. /*
  1208. * This one is used by /dev/mem and fbdev who have no clue about the
  1209. * PCI device, it tries to find the PCI device first and calls the
  1210. * above routine
  1211. */
  1212. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1213. unsigned long pfn,
  1214. unsigned long size,
  1215. pgprot_t protection)
  1216. {
  1217. struct pci_dev *pdev = NULL;
  1218. struct resource *found = NULL;
  1219. unsigned long prot = pgprot_val(protection);
  1220. unsigned long offset = pfn << PAGE_SHIFT;
  1221. int i;
  1222. if (page_is_ram(pfn))
  1223. return prot;
  1224. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1225. for_each_pci_dev(pdev) {
  1226. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1227. struct resource *rp = &pdev->resource[i];
  1228. int flags = rp->flags;
  1229. /* Active and same type? */
  1230. if ((flags & IORESOURCE_MEM) == 0)
  1231. continue;
  1232. /* In the range of this resource? */
  1233. if (offset < (rp->start & PAGE_MASK) ||
  1234. offset > rp->end)
  1235. continue;
  1236. found = rp;
  1237. break;
  1238. }
  1239. if (found)
  1240. break;
  1241. }
  1242. if (found) {
  1243. if (found->flags & IORESOURCE_PREFETCH)
  1244. prot &= ~_PAGE_GUARDED;
  1245. pci_dev_put(pdev);
  1246. }
  1247. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1248. return __pgprot(prot);
  1249. }
  1250. /*
  1251. * Perform the actual remap of the pages for a PCI device mapping, as
  1252. * appropriate for this architecture. The region in the process to map
  1253. * is described by vm_start and vm_end members of VMA, the base physical
  1254. * address is found in vm_pgoff.
  1255. * The pci device structure is provided so that architectures may make mapping
  1256. * decisions on a per-device or per-bus basis.
  1257. *
  1258. * Returns a negative error code on failure, zero on success.
  1259. */
  1260. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1261. enum pci_mmap_state mmap_state,
  1262. int write_combine)
  1263. {
  1264. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1265. struct resource *rp;
  1266. int ret;
  1267. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1268. if (rp == NULL)
  1269. return -EINVAL;
  1270. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1271. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  1272. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1273. vma->vm_page_prot,
  1274. mmap_state, write_combine);
  1275. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1276. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1277. return ret;
  1278. }
  1279. /* Obsolete functions. Should be removed once the symbios driver
  1280. * is fixed
  1281. */
  1282. unsigned long
  1283. phys_to_bus(unsigned long pa)
  1284. {
  1285. struct pci_controller *hose;
  1286. int i;
  1287. for (hose = hose_head; hose; hose = hose->next) {
  1288. for (i = 0; i < 3; ++i) {
  1289. if (pa >= hose->mem_resources[i].start
  1290. && pa <= hose->mem_resources[i].end) {
  1291. /*
  1292. * XXX the hose->pci_mem_offset really
  1293. * only applies to mem_resources[0].
  1294. * We need a way to store an offset for
  1295. * the others. -- paulus
  1296. */
  1297. if (i == 0)
  1298. pa -= hose->pci_mem_offset;
  1299. return pa;
  1300. }
  1301. }
  1302. }
  1303. /* hmmm, didn't find it */
  1304. return 0;
  1305. }
  1306. unsigned long
  1307. pci_phys_to_bus(unsigned long pa, int busnr)
  1308. {
  1309. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1310. if (!hose)
  1311. return pa;
  1312. return pa - hose->pci_mem_offset;
  1313. }
  1314. unsigned long
  1315. pci_bus_to_phys(unsigned int ba, int busnr)
  1316. {
  1317. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1318. if (!hose)
  1319. return ba;
  1320. return ba + hose->pci_mem_offset;
  1321. }
  1322. /* Provide information on locations of various I/O regions in physical
  1323. * memory. Do this on a per-card basis so that we choose the right
  1324. * root bridge.
  1325. * Note that the returned IO or memory base is a physical address
  1326. */
  1327. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1328. {
  1329. struct pci_controller* hose;
  1330. long result = -EOPNOTSUPP;
  1331. hose = pci_bus_to_hose(bus);
  1332. if (!hose)
  1333. return -ENODEV;
  1334. switch (which) {
  1335. case IOBASE_BRIDGE_NUMBER:
  1336. return (long)hose->first_busno;
  1337. case IOBASE_MEMORY:
  1338. return (long)hose->pci_mem_offset;
  1339. case IOBASE_IO:
  1340. return (long)hose->io_base_phys;
  1341. case IOBASE_ISA_IO:
  1342. return (long)isa_io_base;
  1343. case IOBASE_ISA_MEM:
  1344. return (long)isa_mem_base;
  1345. }
  1346. return result;
  1347. }
  1348. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1349. const struct resource *rsrc,
  1350. u64 *start, u64 *end)
  1351. {
  1352. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1353. unsigned long offset = 0;
  1354. if (hose == NULL)
  1355. return;
  1356. if (rsrc->flags & IORESOURCE_IO)
  1357. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1358. *start = rsrc->start + offset;
  1359. *end = rsrc->end + offset;
  1360. }
  1361. void __init
  1362. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1363. int flags, char *name)
  1364. {
  1365. res->start = start;
  1366. res->end = end;
  1367. res->flags = flags;
  1368. res->name = name;
  1369. res->parent = NULL;
  1370. res->sibling = NULL;
  1371. res->child = NULL;
  1372. }
  1373. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1374. {
  1375. unsigned long start = pci_resource_start(dev, bar);
  1376. unsigned long len = pci_resource_len(dev, bar);
  1377. unsigned long flags = pci_resource_flags(dev, bar);
  1378. if (!len)
  1379. return NULL;
  1380. if (max && len > max)
  1381. len = max;
  1382. if (flags & IORESOURCE_IO)
  1383. return ioport_map(start, len);
  1384. if (flags & IORESOURCE_MEM)
  1385. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1386. * not currently distinguish between ioremap and
  1387. * ioremap_nocache.
  1388. */
  1389. return ioremap(start, len);
  1390. /* What? */
  1391. return NULL;
  1392. }
  1393. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1394. {
  1395. /* Nothing to do */
  1396. }
  1397. EXPORT_SYMBOL(pci_iomap);
  1398. EXPORT_SYMBOL(pci_iounmap);
  1399. unsigned long pci_address_to_pio(phys_addr_t address)
  1400. {
  1401. struct pci_controller* hose = hose_head;
  1402. for (; hose; hose = hose->next) {
  1403. unsigned int size = hose->io_resource.end -
  1404. hose->io_resource.start + 1;
  1405. if (address >= hose->io_base_phys &&
  1406. address < (hose->io_base_phys + size)) {
  1407. unsigned long base =
  1408. (unsigned long)hose->io_base_virt - _IO_BASE;
  1409. return base + (address - hose->io_base_phys);
  1410. }
  1411. }
  1412. return (unsigned int)-1;
  1413. }
  1414. EXPORT_SYMBOL(pci_address_to_pio);
  1415. /*
  1416. * Null PCI config access functions, for the case when we can't
  1417. * find a hose.
  1418. */
  1419. #define NULL_PCI_OP(rw, size, type) \
  1420. static int \
  1421. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1422. { \
  1423. return PCIBIOS_DEVICE_NOT_FOUND; \
  1424. }
  1425. static int
  1426. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1427. int len, u32 *val)
  1428. {
  1429. return PCIBIOS_DEVICE_NOT_FOUND;
  1430. }
  1431. static int
  1432. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1433. int len, u32 val)
  1434. {
  1435. return PCIBIOS_DEVICE_NOT_FOUND;
  1436. }
  1437. static struct pci_ops null_pci_ops =
  1438. {
  1439. null_read_config,
  1440. null_write_config
  1441. };
  1442. /*
  1443. * These functions are used early on before PCI scanning is done
  1444. * and all of the pci_dev and pci_bus structures have been created.
  1445. */
  1446. static struct pci_bus *
  1447. fake_pci_bus(struct pci_controller *hose, int busnr)
  1448. {
  1449. static struct pci_bus bus;
  1450. if (hose == 0) {
  1451. hose = pci_bus_to_hose(busnr);
  1452. if (hose == 0)
  1453. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1454. }
  1455. bus.number = busnr;
  1456. bus.sysdata = hose;
  1457. bus.ops = hose? hose->ops: &null_pci_ops;
  1458. return &bus;
  1459. }
  1460. #define EARLY_PCI_OP(rw, size, type) \
  1461. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1462. int devfn, int offset, type value) \
  1463. { \
  1464. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1465. devfn, offset, value); \
  1466. }
  1467. EARLY_PCI_OP(read, byte, u8 *)
  1468. EARLY_PCI_OP(read, word, u16 *)
  1469. EARLY_PCI_OP(read, dword, u32 *)
  1470. EARLY_PCI_OP(write, byte, u8)
  1471. EARLY_PCI_OP(write, word, u16)
  1472. EARLY_PCI_OP(write, dword, u32)