pci_32.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. /*
  2. * Common pmac/prep/chrp pci routines. -- Cort
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/pci.h>
  6. #include <linux/delay.h>
  7. #include <linux/string.h>
  8. #include <linux/init.h>
  9. #include <linux/capability.h>
  10. #include <linux/sched.h>
  11. #include <linux/errno.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/irq.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/uaccess.h>
  21. #include <asm/machdep.h>
  22. #undef DEBUG
  23. #ifdef DEBUG
  24. #define DBG(x...) printk(x)
  25. #else
  26. #define DBG(x...)
  27. #endif
  28. unsigned long isa_io_base = 0;
  29. unsigned long isa_mem_base = 0;
  30. unsigned long pci_dram_offset = 0;
  31. int pcibios_assign_bus_offset = 1;
  32. void pcibios_make_OF_bus_map(void);
  33. static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
  34. static int probe_resource(struct pci_bus *parent, struct resource *pr,
  35. struct resource *res, struct resource **conflict);
  36. static void update_bridge_base(struct pci_bus *bus, int i);
  37. static void pcibios_fixup_resources(struct pci_dev* dev);
  38. static void fixup_broken_pcnet32(struct pci_dev* dev);
  39. static int reparent_resources(struct resource *parent, struct resource *res);
  40. static void fixup_cpc710_pci64(struct pci_dev* dev);
  41. #ifdef CONFIG_PPC_OF
  42. static u8* pci_to_OF_bus_map;
  43. #endif
  44. /* By default, we don't re-assign bus numbers. We do this only on
  45. * some pmacs
  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 [%016llx-%016llx] 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: %llx -> %llx\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,
  156. resource_size_t size, resource_size_t align)
  157. {
  158. struct pci_dev *dev = data;
  159. if (res->flags & IORESOURCE_IO) {
  160. resource_size_t start = res->start;
  161. if (size > 0x100) {
  162. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  163. " (%lld bytes)\n", pci_name(dev),
  164. dev->resource - res, (unsigned long long)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 %llx..%llx (%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 [%llx..%llx] 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 %llx..%llx\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 %llx..%llx\n",
  339. bus->number, i, (unsigned long long)res->start,
  340. (unsigned long long)res->end);
  341. return 0;
  342. }
  343. static int __init
  344. probe_resource(struct pci_bus *parent, struct resource *pr,
  345. struct resource *res, struct resource **conflict)
  346. {
  347. struct pci_bus *bus;
  348. struct pci_dev *dev;
  349. struct resource *r;
  350. int i;
  351. for (r = pr->child; r != NULL; r = r->sibling) {
  352. if (r->end >= res->start && res->end >= r->start) {
  353. *conflict = r;
  354. return 1;
  355. }
  356. }
  357. list_for_each_entry(bus, &parent->children, node) {
  358. for (i = 0; i < 4; ++i) {
  359. if ((r = bus->resource[i]) == NULL)
  360. continue;
  361. if (!r->flags || r->start > r->end || r == res)
  362. continue;
  363. if (pci_find_parent_resource(bus->self, r) != pr)
  364. continue;
  365. if (r->end >= res->start && res->end >= r->start) {
  366. *conflict = r;
  367. return 1;
  368. }
  369. }
  370. }
  371. list_for_each_entry(dev, &parent->devices, bus_list) {
  372. for (i = 0; i < 6; ++i) {
  373. r = &dev->resource[i];
  374. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  375. continue;
  376. if (pci_find_parent_resource(dev, r) != pr)
  377. continue;
  378. if (r->end >= res->start && res->end >= r->start) {
  379. *conflict = r;
  380. return 1;
  381. }
  382. }
  383. }
  384. return 0;
  385. }
  386. static void __init
  387. update_bridge_base(struct pci_bus *bus, int i)
  388. {
  389. struct resource *res = bus->resource[i];
  390. u8 io_base_lo, io_limit_lo;
  391. u16 mem_base, mem_limit;
  392. u16 cmd;
  393. unsigned long start, end, off;
  394. struct pci_dev *dev = bus->self;
  395. struct pci_controller *hose = dev->sysdata;
  396. if (!hose) {
  397. printk("update_bridge_base: no hose?\n");
  398. return;
  399. }
  400. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  401. pci_write_config_word(dev, PCI_COMMAND,
  402. cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
  403. if (res->flags & IORESOURCE_IO) {
  404. off = (unsigned long) hose->io_base_virt - isa_io_base;
  405. start = res->start - off;
  406. end = res->end - off;
  407. io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
  408. io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
  409. if (end > 0xffff)
  410. io_base_lo |= PCI_IO_RANGE_TYPE_32;
  411. else
  412. io_base_lo |= PCI_IO_RANGE_TYPE_16;
  413. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  414. start >> 16);
  415. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  416. end >> 16);
  417. pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
  418. pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
  419. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  420. == IORESOURCE_MEM) {
  421. off = hose->pci_mem_offset;
  422. mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  423. mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
  424. pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
  425. pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
  426. } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
  427. == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
  428. off = hose->pci_mem_offset;
  429. mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
  430. mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
  431. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
  432. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
  433. } else {
  434. DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
  435. pci_name(dev), i, res->flags);
  436. }
  437. pci_write_config_word(dev, PCI_COMMAND, cmd);
  438. }
  439. static inline void alloc_resource(struct pci_dev *dev, int idx)
  440. {
  441. struct resource *pr, *r = &dev->resource[idx];
  442. DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
  443. pci_name(dev), idx, r->start, r->end, r->flags);
  444. pr = pci_find_parent_resource(dev, r);
  445. if (!pr || request_resource(pr, r) < 0) {
  446. printk(KERN_ERR "PCI: Cannot allocate resource region %d"
  447. " of device %s\n", idx, pci_name(dev));
  448. if (pr)
  449. DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
  450. pr, pr->start, pr->end, pr->flags);
  451. /* We'll assign a new address later */
  452. r->flags |= IORESOURCE_UNSET;
  453. r->end -= r->start;
  454. r->start = 0;
  455. }
  456. }
  457. static void __init
  458. pcibios_allocate_resources(int pass)
  459. {
  460. struct pci_dev *dev = NULL;
  461. int idx, disabled;
  462. u16 command;
  463. struct resource *r;
  464. for_each_pci_dev(dev) {
  465. pci_read_config_word(dev, PCI_COMMAND, &command);
  466. for (idx = 0; idx < 6; idx++) {
  467. r = &dev->resource[idx];
  468. if (r->parent) /* Already allocated */
  469. continue;
  470. if (!r->flags || (r->flags & IORESOURCE_UNSET))
  471. continue; /* Not assigned at all */
  472. if (r->flags & IORESOURCE_IO)
  473. disabled = !(command & PCI_COMMAND_IO);
  474. else
  475. disabled = !(command & PCI_COMMAND_MEMORY);
  476. if (pass == disabled)
  477. alloc_resource(dev, idx);
  478. }
  479. if (pass)
  480. continue;
  481. r = &dev->resource[PCI_ROM_RESOURCE];
  482. if (r->flags & IORESOURCE_ROM_ENABLE) {
  483. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  484. u32 reg;
  485. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  486. r->flags &= ~IORESOURCE_ROM_ENABLE;
  487. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  488. pci_write_config_dword(dev, dev->rom_base_reg,
  489. reg & ~PCI_ROM_ADDRESS_ENABLE);
  490. }
  491. }
  492. }
  493. static void __init
  494. pcibios_assign_resources(void)
  495. {
  496. struct pci_dev *dev = NULL;
  497. int idx;
  498. struct resource *r;
  499. for_each_pci_dev(dev) {
  500. int class = dev->class >> 8;
  501. /* Don't touch classless devices and host bridges */
  502. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  503. continue;
  504. for (idx = 0; idx < 6; idx++) {
  505. r = &dev->resource[idx];
  506. /*
  507. * We shall assign a new address to this resource,
  508. * either because the BIOS (sic) forgot to do so
  509. * or because we have decided the old address was
  510. * unusable for some reason.
  511. */
  512. if ((r->flags & IORESOURCE_UNSET) && r->end &&
  513. (!ppc_md.pcibios_enable_device_hook ||
  514. !ppc_md.pcibios_enable_device_hook(dev, 1))) {
  515. r->flags &= ~IORESOURCE_UNSET;
  516. pci_assign_resource(dev, idx);
  517. }
  518. }
  519. #if 0 /* don't assign ROMs */
  520. r = &dev->resource[PCI_ROM_RESOURCE];
  521. r->end -= r->start;
  522. r->start = 0;
  523. if (r->end)
  524. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  525. #endif
  526. }
  527. }
  528. int
  529. pcibios_enable_resources(struct pci_dev *dev, int mask)
  530. {
  531. u16 cmd, old_cmd;
  532. int idx;
  533. struct resource *r;
  534. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  535. old_cmd = cmd;
  536. for (idx=0; idx<6; idx++) {
  537. /* Only set up the requested stuff */
  538. if (!(mask & (1<<idx)))
  539. continue;
  540. r = &dev->resource[idx];
  541. if (r->flags & IORESOURCE_UNSET) {
  542. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  543. return -EINVAL;
  544. }
  545. if (r->flags & IORESOURCE_IO)
  546. cmd |= PCI_COMMAND_IO;
  547. if (r->flags & IORESOURCE_MEM)
  548. cmd |= PCI_COMMAND_MEMORY;
  549. }
  550. if (dev->resource[PCI_ROM_RESOURCE].start)
  551. cmd |= PCI_COMMAND_MEMORY;
  552. if (cmd != old_cmd) {
  553. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  554. pci_write_config_word(dev, PCI_COMMAND, cmd);
  555. }
  556. return 0;
  557. }
  558. static int next_controller_index;
  559. struct pci_controller * __init
  560. pcibios_alloc_controller(void)
  561. {
  562. struct pci_controller *hose;
  563. hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
  564. memset(hose, 0, sizeof(struct pci_controller));
  565. *hose_tail = hose;
  566. hose_tail = &hose->next;
  567. hose->index = next_controller_index++;
  568. return hose;
  569. }
  570. #ifdef CONFIG_PPC_OF
  571. /*
  572. * Functions below are used on OpenFirmware machines.
  573. */
  574. static void
  575. make_one_node_map(struct device_node* node, u8 pci_bus)
  576. {
  577. const int *bus_range;
  578. int len;
  579. if (pci_bus >= pci_bus_count)
  580. return;
  581. bus_range = get_property(node, "bus-range", &len);
  582. if (bus_range == NULL || len < 2 * sizeof(int)) {
  583. printk(KERN_WARNING "Can't get bus-range for %s, "
  584. "assuming it starts at 0\n", node->full_name);
  585. pci_to_OF_bus_map[pci_bus] = 0;
  586. } else
  587. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  588. for (node=node->child; node != 0;node = node->sibling) {
  589. struct pci_dev* dev;
  590. const unsigned int *class_code, *reg;
  591. class_code = get_property(node, "class-code", NULL);
  592. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  593. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  594. continue;
  595. reg = get_property(node, "reg", NULL);
  596. if (!reg)
  597. continue;
  598. dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  599. if (!dev || !dev->subordinate)
  600. continue;
  601. make_one_node_map(node, dev->subordinate->number);
  602. }
  603. }
  604. void
  605. pcibios_make_OF_bus_map(void)
  606. {
  607. int i;
  608. struct pci_controller* hose;
  609. struct property *map_prop;
  610. pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
  611. if (!pci_to_OF_bus_map) {
  612. printk(KERN_ERR "Can't allocate OF bus map !\n");
  613. return;
  614. }
  615. /* We fill the bus map with invalid values, that helps
  616. * debugging.
  617. */
  618. for (i=0; i<pci_bus_count; i++)
  619. pci_to_OF_bus_map[i] = 0xff;
  620. /* For each hose, we begin searching bridges */
  621. for(hose=hose_head; hose; hose=hose->next) {
  622. struct device_node* node;
  623. node = (struct device_node *)hose->arch_data;
  624. if (!node)
  625. continue;
  626. make_one_node_map(node, hose->first_busno);
  627. }
  628. map_prop = of_find_property(find_path_device("/"),
  629. "pci-OF-bus-map", NULL);
  630. if (map_prop) {
  631. BUG_ON(pci_bus_count > map_prop->length);
  632. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  633. }
  634. #ifdef DEBUG
  635. printk("PCI->OF bus map:\n");
  636. for (i=0; i<pci_bus_count; i++) {
  637. if (pci_to_OF_bus_map[i] == 0xff)
  638. continue;
  639. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  640. }
  641. #endif
  642. }
  643. typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
  644. static struct device_node*
  645. scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
  646. {
  647. struct device_node* sub_node;
  648. for (; node != 0;node = node->sibling) {
  649. const unsigned int *class_code;
  650. if (filter(node, data))
  651. return node;
  652. /* For PCI<->PCI bridges or CardBus bridges, we go down
  653. * Note: some OFs create a parent node "multifunc-device" as
  654. * a fake root for all functions of a multi-function device,
  655. * we go down them as well.
  656. */
  657. class_code = get_property(node, "class-code", NULL);
  658. if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  659. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  660. strcmp(node->name, "multifunc-device"))
  661. continue;
  662. sub_node = scan_OF_pci_childs(node->child, filter, data);
  663. if (sub_node)
  664. return sub_node;
  665. }
  666. return NULL;
  667. }
  668. static int
  669. scan_OF_pci_childs_iterator(struct device_node* node, void* data)
  670. {
  671. const unsigned int *reg;
  672. u8* fdata = (u8*)data;
  673. reg = get_property(node, "reg", NULL);
  674. if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
  675. && ((reg[0] >> 16) & 0xff) == fdata[0])
  676. return 1;
  677. return 0;
  678. }
  679. static struct device_node*
  680. scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
  681. {
  682. u8 filter_data[2] = {bus, dev_fn};
  683. return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
  684. }
  685. /*
  686. * Scans the OF tree for a device node matching a PCI device
  687. */
  688. struct device_node *
  689. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  690. {
  691. struct pci_controller *hose;
  692. struct device_node *node;
  693. int busnr;
  694. if (!have_of)
  695. return NULL;
  696. /* Lookup the hose */
  697. busnr = bus->number;
  698. hose = pci_bus_to_hose(busnr);
  699. if (!hose)
  700. return NULL;
  701. /* Check it has an OF node associated */
  702. node = (struct device_node *) hose->arch_data;
  703. if (!node)
  704. return NULL;
  705. /* Fixup bus number according to what OF think it is. */
  706. #ifdef CONFIG_PPC_PMAC
  707. /* The G5 need a special case here. Basically, we don't remap all
  708. * busses on it so we don't create the pci-OF-map. However, we do
  709. * remap the AGP bus and so have to deal with it. A future better
  710. * fix has to be done by making the remapping per-host and always
  711. * filling the pci_to_OF map. --BenH
  712. */
  713. if (machine_is(powermac) && busnr >= 0xf0)
  714. busnr -= 0xf0;
  715. else
  716. #endif
  717. if (pci_to_OF_bus_map)
  718. busnr = pci_to_OF_bus_map[busnr];
  719. if (busnr == 0xff)
  720. return NULL;
  721. /* Now, lookup childs of the hose */
  722. return scan_OF_childs_for_device(node->child, busnr, devfn);
  723. }
  724. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  725. struct device_node*
  726. pci_device_to_OF_node(struct pci_dev *dev)
  727. {
  728. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  729. }
  730. EXPORT_SYMBOL(pci_device_to_OF_node);
  731. /* This routine is meant to be used early during boot, when the
  732. * PCI bus numbers have not yet been assigned, and you need to
  733. * issue PCI config cycles to an OF device.
  734. * It could also be used to "fix" RTAS config cycles if you want
  735. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  736. * config cycles.
  737. */
  738. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  739. {
  740. if (!have_of)
  741. return NULL;
  742. while(node) {
  743. struct pci_controller* hose;
  744. for (hose=hose_head;hose;hose=hose->next)
  745. if (hose->arch_data == node)
  746. return hose;
  747. node=node->parent;
  748. }
  749. return NULL;
  750. }
  751. static int
  752. find_OF_pci_device_filter(struct device_node* node, void* data)
  753. {
  754. return ((void *)node == data);
  755. }
  756. /*
  757. * Returns the PCI device matching a given OF node
  758. */
  759. int
  760. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  761. {
  762. const unsigned int *reg;
  763. struct pci_controller* hose;
  764. struct pci_dev* dev = NULL;
  765. if (!have_of)
  766. return -ENODEV;
  767. /* Make sure it's really a PCI device */
  768. hose = pci_find_hose_for_OF_device(node);
  769. if (!hose || !hose->arch_data)
  770. return -ENODEV;
  771. if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
  772. find_OF_pci_device_filter, (void *)node))
  773. return -ENODEV;
  774. reg = get_property(node, "reg", NULL);
  775. if (!reg)
  776. return -ENODEV;
  777. *bus = (reg[0] >> 16) & 0xff;
  778. *devfn = ((reg[0] >> 8) & 0xff);
  779. /* Ok, here we need some tweak. If we have already renumbered
  780. * all busses, we can't rely on the OF bus number any more.
  781. * the pci_to_OF_bus_map is not enough as several PCI busses
  782. * may match the same OF bus number.
  783. */
  784. if (!pci_to_OF_bus_map)
  785. return 0;
  786. for_each_pci_dev(dev)
  787. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  788. dev->devfn == *devfn) {
  789. *bus = dev->bus->number;
  790. pci_dev_put(dev);
  791. return 0;
  792. }
  793. return -ENODEV;
  794. }
  795. EXPORT_SYMBOL(pci_device_from_OF_node);
  796. void __init
  797. pci_process_bridge_OF_ranges(struct pci_controller *hose,
  798. struct device_node *dev, int primary)
  799. {
  800. static unsigned int static_lc_ranges[256] __initdata;
  801. const unsigned int *dt_ranges;
  802. unsigned int *lc_ranges, *ranges, *prev, size;
  803. int rlen = 0, orig_rlen;
  804. int memno = 0;
  805. struct resource *res;
  806. int np, na = prom_n_addr_cells(dev);
  807. np = na + 5;
  808. /* First we try to merge ranges to fix a problem with some pmacs
  809. * that can have more than 3 ranges, fortunately using contiguous
  810. * addresses -- BenH
  811. */
  812. dt_ranges = get_property(dev, "ranges", &rlen);
  813. if (!dt_ranges)
  814. return;
  815. /* Sanity check, though hopefully that never happens */
  816. if (rlen > sizeof(static_lc_ranges)) {
  817. printk(KERN_WARNING "OF ranges property too large !\n");
  818. rlen = sizeof(static_lc_ranges);
  819. }
  820. lc_ranges = static_lc_ranges;
  821. memcpy(lc_ranges, dt_ranges, rlen);
  822. orig_rlen = rlen;
  823. /* Let's work on a copy of the "ranges" property instead of damaging
  824. * the device-tree image in memory
  825. */
  826. ranges = lc_ranges;
  827. prev = NULL;
  828. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  829. if (prev) {
  830. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  831. (prev[2] + prev[na+4]) == ranges[2] &&
  832. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  833. prev[na+4] += ranges[na+4];
  834. ranges[0] = 0;
  835. ranges += np;
  836. continue;
  837. }
  838. }
  839. prev = ranges;
  840. ranges += np;
  841. }
  842. /*
  843. * The ranges property is laid out as an array of elements,
  844. * each of which comprises:
  845. * cells 0 - 2: a PCI address
  846. * cells 3 or 3+4: a CPU physical address
  847. * (size depending on dev->n_addr_cells)
  848. * cells 4+5 or 5+6: the size of the range
  849. */
  850. ranges = lc_ranges;
  851. rlen = orig_rlen;
  852. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  853. res = NULL;
  854. size = ranges[na+4];
  855. switch ((ranges[0] >> 24) & 0x3) {
  856. case 1: /* I/O space */
  857. if (ranges[2] != 0)
  858. break;
  859. hose->io_base_phys = ranges[na+2];
  860. /* limit I/O space to 16MB */
  861. if (size > 0x01000000)
  862. size = 0x01000000;
  863. hose->io_base_virt = ioremap(ranges[na+2], size);
  864. if (primary)
  865. isa_io_base = (unsigned long) hose->io_base_virt;
  866. res = &hose->io_resource;
  867. res->flags = IORESOURCE_IO;
  868. res->start = ranges[2];
  869. DBG("PCI: IO 0x%llx -> 0x%llx\n",
  870. res->start, res->start + size - 1);
  871. break;
  872. case 2: /* memory space */
  873. memno = 0;
  874. if (ranges[1] == 0 && ranges[2] == 0
  875. && ranges[na+4] <= (16 << 20)) {
  876. /* 1st 16MB, i.e. ISA memory area */
  877. if (primary)
  878. isa_mem_base = ranges[na+2];
  879. memno = 1;
  880. }
  881. while (memno < 3 && hose->mem_resources[memno].flags)
  882. ++memno;
  883. if (memno == 0)
  884. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  885. if (memno < 3) {
  886. res = &hose->mem_resources[memno];
  887. res->flags = IORESOURCE_MEM;
  888. if(ranges[0] & 0x40000000)
  889. res->flags |= IORESOURCE_PREFETCH;
  890. res->start = ranges[na+2];
  891. DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
  892. res->start, res->start + size - 1);
  893. }
  894. break;
  895. }
  896. if (res != NULL) {
  897. res->name = dev->full_name;
  898. res->end = res->start + size - 1;
  899. res->parent = NULL;
  900. res->sibling = NULL;
  901. res->child = NULL;
  902. }
  903. ranges += np;
  904. }
  905. }
  906. /* We create the "pci-OF-bus-map" property now so it appears in the
  907. * /proc device tree
  908. */
  909. void __init
  910. pci_create_OF_bus_map(void)
  911. {
  912. struct property* of_prop;
  913. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  914. if (of_prop && find_path_device("/")) {
  915. memset(of_prop, -1, sizeof(struct property) + 256);
  916. of_prop->name = "pci-OF-bus-map";
  917. of_prop->length = 256;
  918. of_prop->value = (unsigned char *)&of_prop[1];
  919. prom_add_property(find_path_device("/"), of_prop);
  920. }
  921. }
  922. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  923. {
  924. struct pci_dev *pdev;
  925. struct device_node *np;
  926. pdev = to_pci_dev (dev);
  927. np = pci_device_to_OF_node(pdev);
  928. if (np == NULL || np->full_name == NULL)
  929. return 0;
  930. return sprintf(buf, "%s", np->full_name);
  931. }
  932. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  933. #else /* CONFIG_PPC_OF */
  934. void pcibios_make_OF_bus_map(void)
  935. {
  936. }
  937. #endif /* CONFIG_PPC_OF */
  938. /* Add sysfs properties */
  939. void pcibios_add_platform_entries(struct pci_dev *pdev)
  940. {
  941. #ifdef CONFIG_PPC_OF
  942. device_create_file(&pdev->dev, &dev_attr_devspec);
  943. #endif /* CONFIG_PPC_OF */
  944. }
  945. #ifdef CONFIG_PPC_PMAC
  946. /*
  947. * This set of routines checks for PCI<->PCI bridges that have closed
  948. * IO resources and have child devices. It tries to re-open an IO
  949. * window on them.
  950. *
  951. * This is a _temporary_ fix to workaround a problem with Apple's OF
  952. * closing IO windows on P2P bridges when the OF drivers of cards
  953. * below this bridge don't claim any IO range (typically ATI or
  954. * Adaptec).
  955. *
  956. * A more complete fix would be to use drivers/pci/setup-bus.c, which
  957. * involves a working pcibios_fixup_pbus_ranges(), some more care about
  958. * ordering when creating the host bus resources, and maybe a few more
  959. * minor tweaks
  960. */
  961. /* Initialize bridges with base/limit values we have collected */
  962. static void __init
  963. do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
  964. {
  965. struct pci_dev *bridge = bus->self;
  966. struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
  967. u32 l;
  968. u16 w;
  969. struct resource res;
  970. if (bus->resource[0] == NULL)
  971. return;
  972. res = *(bus->resource[0]);
  973. DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
  974. res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
  975. res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
  976. DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
  977. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  978. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  979. l &= 0xffff000f;
  980. l |= (res.start >> 8) & 0x00f0;
  981. l |= res.end & 0xf000;
  982. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  983. if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  984. l = (res.start >> 16) | (res.end & 0xffff0000);
  985. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
  986. }
  987. pci_read_config_word(bridge, PCI_COMMAND, &w);
  988. w |= PCI_COMMAND_IO;
  989. pci_write_config_word(bridge, PCI_COMMAND, w);
  990. #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
  991. if (enable_vga) {
  992. pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
  993. w |= PCI_BRIDGE_CTL_VGA;
  994. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
  995. }
  996. #endif
  997. }
  998. /* This function is pretty basic and actually quite broken for the
  999. * general case, it's enough for us right now though. It's supposed
  1000. * to tell us if we need to open an IO range at all or not and what
  1001. * size.
  1002. */
  1003. static int __init
  1004. check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
  1005. {
  1006. struct pci_dev *dev;
  1007. int i;
  1008. int rc = 0;
  1009. #define push_end(res, mask) do { \
  1010. BUG_ON((mask+1) & mask); \
  1011. res->end = (res->end + mask) | mask; \
  1012. } while (0)
  1013. list_for_each_entry(dev, &bus->devices, bus_list) {
  1014. u16 class = dev->class >> 8;
  1015. if (class == PCI_CLASS_DISPLAY_VGA ||
  1016. class == PCI_CLASS_NOT_DEFINED_VGA)
  1017. *found_vga = 1;
  1018. if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
  1019. rc |= check_for_io_childs(dev->subordinate, res, found_vga);
  1020. if (class == PCI_CLASS_BRIDGE_CARDBUS)
  1021. push_end(res, 0xfff);
  1022. for (i=0; i<PCI_NUM_RESOURCES; i++) {
  1023. struct resource *r;
  1024. unsigned long r_size;
  1025. if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
  1026. && i >= PCI_BRIDGE_RESOURCES)
  1027. continue;
  1028. r = &dev->resource[i];
  1029. r_size = r->end - r->start;
  1030. if (r_size < 0xfff)
  1031. r_size = 0xfff;
  1032. if (r->flags & IORESOURCE_IO && (r_size) != 0) {
  1033. rc = 1;
  1034. push_end(res, r_size);
  1035. }
  1036. }
  1037. }
  1038. return rc;
  1039. }
  1040. /* Here we scan all P2P bridges of a given level that have a closed
  1041. * IO window. Note that the test for the presence of a VGA card should
  1042. * be improved to take into account already configured P2P bridges,
  1043. * currently, we don't see them and might end up configuring 2 bridges
  1044. * with VGA pass through enabled
  1045. */
  1046. static void __init
  1047. do_fixup_p2p_level(struct pci_bus *bus)
  1048. {
  1049. struct pci_bus *b;
  1050. int i, parent_io;
  1051. int has_vga = 0;
  1052. for (parent_io=0; parent_io<4; parent_io++)
  1053. if (bus->resource[parent_io]
  1054. && bus->resource[parent_io]->flags & IORESOURCE_IO)
  1055. break;
  1056. if (parent_io >= 4)
  1057. return;
  1058. list_for_each_entry(b, &bus->children, node) {
  1059. struct pci_dev *d = b->self;
  1060. struct pci_controller* hose = (struct pci_controller *)d->sysdata;
  1061. struct resource *res = b->resource[0];
  1062. struct resource tmp_res;
  1063. unsigned long max;
  1064. int found_vga = 0;
  1065. memset(&tmp_res, 0, sizeof(tmp_res));
  1066. tmp_res.start = bus->resource[parent_io]->start;
  1067. /* We don't let low addresses go through that closed P2P bridge, well,
  1068. * that may not be necessary but I feel safer that way
  1069. */
  1070. if (tmp_res.start == 0)
  1071. tmp_res.start = 0x1000;
  1072. if (!list_empty(&b->devices) && res && res->flags == 0 &&
  1073. res != bus->resource[parent_io] &&
  1074. (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  1075. check_for_io_childs(b, &tmp_res, &found_vga)) {
  1076. u8 io_base_lo;
  1077. printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
  1078. if (found_vga) {
  1079. if (has_vga) {
  1080. printk(KERN_WARNING "Skipping VGA, already active"
  1081. " on bus segment\n");
  1082. found_vga = 0;
  1083. } else
  1084. has_vga = 1;
  1085. }
  1086. pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
  1087. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
  1088. max = ((unsigned long) hose->io_base_virt
  1089. - isa_io_base) + 0xffffffff;
  1090. else
  1091. max = ((unsigned long) hose->io_base_virt
  1092. - isa_io_base) + 0xffff;
  1093. *res = tmp_res;
  1094. res->flags = IORESOURCE_IO;
  1095. res->name = b->name;
  1096. /* Find a resource in the parent where we can allocate */
  1097. for (i = 0 ; i < 4; i++) {
  1098. struct resource *r = bus->resource[i];
  1099. if (!r)
  1100. continue;
  1101. if ((r->flags & IORESOURCE_IO) == 0)
  1102. continue;
  1103. DBG("Trying to allocate from %016llx, size %016llx from parent"
  1104. " res %d: %016llx -> %016llx\n",
  1105. res->start, res->end, i, r->start, r->end);
  1106. if (allocate_resource(r, res, res->end + 1, res->start, max,
  1107. res->end + 1, NULL, NULL) < 0) {
  1108. DBG("Failed !\n");
  1109. continue;
  1110. }
  1111. do_update_p2p_io_resource(b, found_vga);
  1112. break;
  1113. }
  1114. }
  1115. do_fixup_p2p_level(b);
  1116. }
  1117. }
  1118. static void
  1119. pcibios_fixup_p2p_bridges(void)
  1120. {
  1121. struct pci_bus *b;
  1122. list_for_each_entry(b, &pci_root_buses, node)
  1123. do_fixup_p2p_level(b);
  1124. }
  1125. #endif /* CONFIG_PPC_PMAC */
  1126. static int __init
  1127. pcibios_init(void)
  1128. {
  1129. struct pci_controller *hose;
  1130. struct pci_bus *bus;
  1131. int next_busno;
  1132. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  1133. /* Scan all of the recorded PCI controllers. */
  1134. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  1135. if (pci_assign_all_buses)
  1136. hose->first_busno = next_busno;
  1137. hose->last_busno = 0xff;
  1138. bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
  1139. hose->last_busno = bus->subordinate;
  1140. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  1141. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  1142. }
  1143. pci_bus_count = next_busno;
  1144. /* OpenFirmware based machines need a map of OF bus
  1145. * numbers vs. kernel bus numbers since we may have to
  1146. * remap them.
  1147. */
  1148. if (pci_assign_all_buses && have_of)
  1149. pcibios_make_OF_bus_map();
  1150. /* Do machine dependent PCI interrupt routing */
  1151. if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
  1152. pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
  1153. /* Call machine dependent fixup */
  1154. if (ppc_md.pcibios_fixup)
  1155. ppc_md.pcibios_fixup();
  1156. /* Allocate and assign resources */
  1157. pcibios_allocate_bus_resources(&pci_root_buses);
  1158. pcibios_allocate_resources(0);
  1159. pcibios_allocate_resources(1);
  1160. #ifdef CONFIG_PPC_PMAC
  1161. pcibios_fixup_p2p_bridges();
  1162. #endif /* CONFIG_PPC_PMAC */
  1163. pcibios_assign_resources();
  1164. /* Call machine dependent post-init code */
  1165. if (ppc_md.pcibios_after_init)
  1166. ppc_md.pcibios_after_init();
  1167. return 0;
  1168. }
  1169. subsys_initcall(pcibios_init);
  1170. unsigned char __init
  1171. common_swizzle(struct pci_dev *dev, unsigned char *pinp)
  1172. {
  1173. struct pci_controller *hose = dev->sysdata;
  1174. if (dev->bus->number != hose->first_busno) {
  1175. u8 pin = *pinp;
  1176. do {
  1177. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  1178. /* Move up the chain of bridges. */
  1179. dev = dev->bus->self;
  1180. } while (dev->bus->self);
  1181. *pinp = pin;
  1182. /* The slot is the idsel of the last bridge. */
  1183. }
  1184. return PCI_SLOT(dev->devfn);
  1185. }
  1186. unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
  1187. unsigned long start, unsigned long size)
  1188. {
  1189. return start;
  1190. }
  1191. void __init pcibios_fixup_bus(struct pci_bus *bus)
  1192. {
  1193. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1194. unsigned long io_offset;
  1195. struct resource *res;
  1196. int i;
  1197. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  1198. if (bus->parent == NULL) {
  1199. /* This is a host bridge - fill in its resources */
  1200. hose->bus = bus;
  1201. bus->resource[0] = res = &hose->io_resource;
  1202. if (!res->flags) {
  1203. if (io_offset)
  1204. printk(KERN_ERR "I/O resource not set for host"
  1205. " bridge %d\n", hose->index);
  1206. res->start = 0;
  1207. res->end = IO_SPACE_LIMIT;
  1208. res->flags = IORESOURCE_IO;
  1209. }
  1210. res->start += io_offset;
  1211. res->end += io_offset;
  1212. for (i = 0; i < 3; ++i) {
  1213. res = &hose->mem_resources[i];
  1214. if (!res->flags) {
  1215. if (i > 0)
  1216. continue;
  1217. printk(KERN_ERR "Memory resource not set for "
  1218. "host bridge %d\n", hose->index);
  1219. res->start = hose->pci_mem_offset;
  1220. res->end = ~0U;
  1221. res->flags = IORESOURCE_MEM;
  1222. }
  1223. bus->resource[i+1] = res;
  1224. }
  1225. } else {
  1226. /* This is a subordinate bridge */
  1227. pci_read_bridge_bases(bus);
  1228. for (i = 0; i < 4; ++i) {
  1229. if ((res = bus->resource[i]) == NULL)
  1230. continue;
  1231. if (!res->flags)
  1232. continue;
  1233. if (io_offset && (res->flags & IORESOURCE_IO)) {
  1234. res->start += io_offset;
  1235. res->end += io_offset;
  1236. } else if (hose->pci_mem_offset
  1237. && (res->flags & IORESOURCE_MEM)) {
  1238. res->start += hose->pci_mem_offset;
  1239. res->end += hose->pci_mem_offset;
  1240. }
  1241. }
  1242. }
  1243. if (ppc_md.pcibios_fixup_bus)
  1244. ppc_md.pcibios_fixup_bus(bus);
  1245. }
  1246. char __init *pcibios_setup(char *str)
  1247. {
  1248. return str;
  1249. }
  1250. /* the next one is stolen from the alpha port... */
  1251. void __init
  1252. pcibios_update_irq(struct pci_dev *dev, int irq)
  1253. {
  1254. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  1255. /* XXX FIXME - update OF device tree node interrupt property */
  1256. }
  1257. #ifdef CONFIG_PPC_MERGE
  1258. /* XXX This is a copy of the ppc64 version. This is temporary until we start
  1259. * merging the 2 PCI layers
  1260. */
  1261. /*
  1262. * Reads the interrupt pin to determine if interrupt is use by card.
  1263. * If the interrupt is used, then gets the interrupt line from the
  1264. * openfirmware and sets it in the pci_dev and pci_config line.
  1265. */
  1266. int pci_read_irq_line(struct pci_dev *pci_dev)
  1267. {
  1268. struct of_irq oirq;
  1269. unsigned int virq;
  1270. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  1271. /* Try to get a mapping from the device-tree */
  1272. if (of_irq_map_pci(pci_dev, &oirq)) {
  1273. u8 line, pin;
  1274. /* If that fails, lets fallback to what is in the config
  1275. * space and map that through the default controller. We
  1276. * also set the type to level low since that's what PCI
  1277. * interrupts are. If your platform does differently, then
  1278. * either provide a proper interrupt tree or don't use this
  1279. * function.
  1280. */
  1281. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  1282. return -1;
  1283. if (pin == 0)
  1284. return -1;
  1285. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  1286. line == 0xff) {
  1287. return -1;
  1288. }
  1289. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  1290. virq = irq_create_mapping(NULL, line);
  1291. if (virq != NO_IRQ)
  1292. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  1293. } else {
  1294. DBG(" -> got one, spec %d cells (0x%08x...) on %s\n",
  1295. oirq.size, oirq.specifier[0], oirq.controller->full_name);
  1296. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  1297. oirq.size);
  1298. }
  1299. if(virq == NO_IRQ) {
  1300. DBG(" -> failed to map !\n");
  1301. return -1;
  1302. }
  1303. pci_dev->irq = virq;
  1304. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, virq);
  1305. return 0;
  1306. }
  1307. EXPORT_SYMBOL(pci_read_irq_line);
  1308. #endif /* CONFIG_PPC_MERGE */
  1309. int pcibios_enable_device(struct pci_dev *dev, int mask)
  1310. {
  1311. u16 cmd, old_cmd;
  1312. int idx;
  1313. struct resource *r;
  1314. if (ppc_md.pcibios_enable_device_hook)
  1315. if (ppc_md.pcibios_enable_device_hook(dev, 0))
  1316. return -EINVAL;
  1317. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1318. old_cmd = cmd;
  1319. for (idx=0; idx<6; idx++) {
  1320. r = &dev->resource[idx];
  1321. if (r->flags & IORESOURCE_UNSET) {
  1322. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  1323. return -EINVAL;
  1324. }
  1325. if (r->flags & IORESOURCE_IO)
  1326. cmd |= PCI_COMMAND_IO;
  1327. if (r->flags & IORESOURCE_MEM)
  1328. cmd |= PCI_COMMAND_MEMORY;
  1329. }
  1330. if (cmd != old_cmd) {
  1331. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  1332. pci_name(dev), old_cmd, cmd);
  1333. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1334. }
  1335. return 0;
  1336. }
  1337. struct pci_controller*
  1338. pci_bus_to_hose(int bus)
  1339. {
  1340. struct pci_controller* hose = hose_head;
  1341. for (; hose; hose = hose->next)
  1342. if (bus >= hose->first_busno && bus <= hose->last_busno)
  1343. return hose;
  1344. return NULL;
  1345. }
  1346. void __iomem *
  1347. pci_bus_io_base(unsigned int bus)
  1348. {
  1349. struct pci_controller *hose;
  1350. hose = pci_bus_to_hose(bus);
  1351. if (!hose)
  1352. return NULL;
  1353. return hose->io_base_virt;
  1354. }
  1355. unsigned long
  1356. pci_bus_io_base_phys(unsigned int bus)
  1357. {
  1358. struct pci_controller *hose;
  1359. hose = pci_bus_to_hose(bus);
  1360. if (!hose)
  1361. return 0;
  1362. return hose->io_base_phys;
  1363. }
  1364. unsigned long
  1365. pci_bus_mem_base_phys(unsigned int bus)
  1366. {
  1367. struct pci_controller *hose;
  1368. hose = pci_bus_to_hose(bus);
  1369. if (!hose)
  1370. return 0;
  1371. return hose->pci_mem_offset;
  1372. }
  1373. unsigned long
  1374. pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
  1375. {
  1376. /* Hack alert again ! See comments in chrp_pci.c
  1377. */
  1378. struct pci_controller* hose =
  1379. (struct pci_controller *)pdev->sysdata;
  1380. if (hose && res->flags & IORESOURCE_MEM)
  1381. return res->start - hose->pci_mem_offset;
  1382. /* We may want to do something with IOs here... */
  1383. return res->start;
  1384. }
  1385. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  1386. unsigned long *offset,
  1387. enum pci_mmap_state mmap_state)
  1388. {
  1389. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1390. unsigned long io_offset = 0;
  1391. int i, res_bit;
  1392. if (hose == 0)
  1393. return NULL; /* should never happen */
  1394. /* If memory, add on the PCI bridge address offset */
  1395. if (mmap_state == pci_mmap_mem) {
  1396. *offset += hose->pci_mem_offset;
  1397. res_bit = IORESOURCE_MEM;
  1398. } else {
  1399. io_offset = hose->io_base_virt - ___IO_BASE;
  1400. *offset += io_offset;
  1401. res_bit = IORESOURCE_IO;
  1402. }
  1403. /*
  1404. * Check that the offset requested corresponds to one of the
  1405. * resources of the device.
  1406. */
  1407. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1408. struct resource *rp = &dev->resource[i];
  1409. int flags = rp->flags;
  1410. /* treat ROM as memory (should be already) */
  1411. if (i == PCI_ROM_RESOURCE)
  1412. flags |= IORESOURCE_MEM;
  1413. /* Active and same type? */
  1414. if ((flags & res_bit) == 0)
  1415. continue;
  1416. /* In the range of this resource? */
  1417. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  1418. continue;
  1419. /* found it! construct the final physical address */
  1420. if (mmap_state == pci_mmap_io)
  1421. *offset += hose->io_base_phys - io_offset;
  1422. return rp;
  1423. }
  1424. return NULL;
  1425. }
  1426. /*
  1427. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  1428. * device mapping.
  1429. */
  1430. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  1431. pgprot_t protection,
  1432. enum pci_mmap_state mmap_state,
  1433. int write_combine)
  1434. {
  1435. unsigned long prot = pgprot_val(protection);
  1436. /* Write combine is always 0 on non-memory space mappings. On
  1437. * memory space, if the user didn't pass 1, we check for a
  1438. * "prefetchable" resource. This is a bit hackish, but we use
  1439. * this to workaround the inability of /sysfs to provide a write
  1440. * combine bit
  1441. */
  1442. if (mmap_state != pci_mmap_mem)
  1443. write_combine = 0;
  1444. else if (write_combine == 0) {
  1445. if (rp->flags & IORESOURCE_PREFETCH)
  1446. write_combine = 1;
  1447. }
  1448. /* XXX would be nice to have a way to ask for write-through */
  1449. prot |= _PAGE_NO_CACHE;
  1450. if (write_combine)
  1451. prot &= ~_PAGE_GUARDED;
  1452. else
  1453. prot |= _PAGE_GUARDED;
  1454. printk("PCI map for %s:%llx, prot: %lx\n", pci_name(dev),
  1455. (unsigned long long)rp->start, prot);
  1456. return __pgprot(prot);
  1457. }
  1458. /*
  1459. * This one is used by /dev/mem and fbdev who have no clue about the
  1460. * PCI device, it tries to find the PCI device first and calls the
  1461. * above routine
  1462. */
  1463. pgprot_t pci_phys_mem_access_prot(struct file *file,
  1464. unsigned long pfn,
  1465. unsigned long size,
  1466. pgprot_t protection)
  1467. {
  1468. struct pci_dev *pdev = NULL;
  1469. struct resource *found = NULL;
  1470. unsigned long prot = pgprot_val(protection);
  1471. unsigned long offset = pfn << PAGE_SHIFT;
  1472. int i;
  1473. if (page_is_ram(pfn))
  1474. return prot;
  1475. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  1476. for_each_pci_dev(pdev) {
  1477. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  1478. struct resource *rp = &pdev->resource[i];
  1479. int flags = rp->flags;
  1480. /* Active and same type? */
  1481. if ((flags & IORESOURCE_MEM) == 0)
  1482. continue;
  1483. /* In the range of this resource? */
  1484. if (offset < (rp->start & PAGE_MASK) ||
  1485. offset > rp->end)
  1486. continue;
  1487. found = rp;
  1488. break;
  1489. }
  1490. if (found)
  1491. break;
  1492. }
  1493. if (found) {
  1494. if (found->flags & IORESOURCE_PREFETCH)
  1495. prot &= ~_PAGE_GUARDED;
  1496. pci_dev_put(pdev);
  1497. }
  1498. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  1499. return __pgprot(prot);
  1500. }
  1501. /*
  1502. * Perform the actual remap of the pages for a PCI device mapping, as
  1503. * appropriate for this architecture. The region in the process to map
  1504. * is described by vm_start and vm_end members of VMA, the base physical
  1505. * address is found in vm_pgoff.
  1506. * The pci device structure is provided so that architectures may make mapping
  1507. * decisions on a per-device or per-bus basis.
  1508. *
  1509. * Returns a negative error code on failure, zero on success.
  1510. */
  1511. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  1512. enum pci_mmap_state mmap_state,
  1513. int write_combine)
  1514. {
  1515. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  1516. struct resource *rp;
  1517. int ret;
  1518. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  1519. if (rp == NULL)
  1520. return -EINVAL;
  1521. vma->vm_pgoff = offset >> PAGE_SHIFT;
  1522. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  1523. vma->vm_page_prot,
  1524. mmap_state, write_combine);
  1525. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  1526. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1527. return ret;
  1528. }
  1529. /* Obsolete functions. Should be removed once the symbios driver
  1530. * is fixed
  1531. */
  1532. unsigned long
  1533. phys_to_bus(unsigned long pa)
  1534. {
  1535. struct pci_controller *hose;
  1536. int i;
  1537. for (hose = hose_head; hose; hose = hose->next) {
  1538. for (i = 0; i < 3; ++i) {
  1539. if (pa >= hose->mem_resources[i].start
  1540. && pa <= hose->mem_resources[i].end) {
  1541. /*
  1542. * XXX the hose->pci_mem_offset really
  1543. * only applies to mem_resources[0].
  1544. * We need a way to store an offset for
  1545. * the others. -- paulus
  1546. */
  1547. if (i == 0)
  1548. pa -= hose->pci_mem_offset;
  1549. return pa;
  1550. }
  1551. }
  1552. }
  1553. /* hmmm, didn't find it */
  1554. return 0;
  1555. }
  1556. unsigned long
  1557. pci_phys_to_bus(unsigned long pa, int busnr)
  1558. {
  1559. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1560. if (!hose)
  1561. return pa;
  1562. return pa - hose->pci_mem_offset;
  1563. }
  1564. unsigned long
  1565. pci_bus_to_phys(unsigned int ba, int busnr)
  1566. {
  1567. struct pci_controller* hose = pci_bus_to_hose(busnr);
  1568. if (!hose)
  1569. return ba;
  1570. return ba + hose->pci_mem_offset;
  1571. }
  1572. /* Provide information on locations of various I/O regions in physical
  1573. * memory. Do this on a per-card basis so that we choose the right
  1574. * root bridge.
  1575. * Note that the returned IO or memory base is a physical address
  1576. */
  1577. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  1578. {
  1579. struct pci_controller* hose;
  1580. long result = -EOPNOTSUPP;
  1581. /* Argh ! Please forgive me for that hack, but that's the
  1582. * simplest way to get existing XFree to not lockup on some
  1583. * G5 machines... So when something asks for bus 0 io base
  1584. * (bus 0 is HT root), we return the AGP one instead.
  1585. */
  1586. #ifdef CONFIG_PPC_PMAC
  1587. if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
  1588. if (bus == 0)
  1589. bus = 0xf0;
  1590. #endif /* CONFIG_PPC_PMAC */
  1591. hose = pci_bus_to_hose(bus);
  1592. if (!hose)
  1593. return -ENODEV;
  1594. switch (which) {
  1595. case IOBASE_BRIDGE_NUMBER:
  1596. return (long)hose->first_busno;
  1597. case IOBASE_MEMORY:
  1598. return (long)hose->pci_mem_offset;
  1599. case IOBASE_IO:
  1600. return (long)hose->io_base_phys;
  1601. case IOBASE_ISA_IO:
  1602. return (long)isa_io_base;
  1603. case IOBASE_ISA_MEM:
  1604. return (long)isa_mem_base;
  1605. }
  1606. return result;
  1607. }
  1608. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1609. const struct resource *rsrc,
  1610. resource_size_t *start, resource_size_t *end)
  1611. {
  1612. struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
  1613. unsigned long offset = 0;
  1614. if (hose == NULL)
  1615. return;
  1616. if (rsrc->flags & IORESOURCE_IO)
  1617. offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
  1618. *start = rsrc->start + offset;
  1619. *end = rsrc->end + offset;
  1620. }
  1621. void __init
  1622. pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
  1623. int flags, char *name)
  1624. {
  1625. res->start = start;
  1626. res->end = end;
  1627. res->flags = flags;
  1628. res->name = name;
  1629. res->parent = NULL;
  1630. res->sibling = NULL;
  1631. res->child = NULL;
  1632. }
  1633. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  1634. {
  1635. unsigned long start = pci_resource_start(dev, bar);
  1636. unsigned long len = pci_resource_len(dev, bar);
  1637. unsigned long flags = pci_resource_flags(dev, bar);
  1638. if (!len)
  1639. return NULL;
  1640. if (max && len > max)
  1641. len = max;
  1642. if (flags & IORESOURCE_IO)
  1643. return ioport_map(start, len);
  1644. if (flags & IORESOURCE_MEM)
  1645. /* Not checking IORESOURCE_CACHEABLE because PPC does
  1646. * not currently distinguish between ioremap and
  1647. * ioremap_nocache.
  1648. */
  1649. return ioremap(start, len);
  1650. /* What? */
  1651. return NULL;
  1652. }
  1653. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1654. {
  1655. /* Nothing to do */
  1656. }
  1657. EXPORT_SYMBOL(pci_iomap);
  1658. EXPORT_SYMBOL(pci_iounmap);
  1659. unsigned long pci_address_to_pio(phys_addr_t address)
  1660. {
  1661. struct pci_controller* hose = hose_head;
  1662. for (; hose; hose = hose->next) {
  1663. unsigned int size = hose->io_resource.end -
  1664. hose->io_resource.start + 1;
  1665. if (address >= hose->io_base_phys &&
  1666. address < (hose->io_base_phys + size)) {
  1667. unsigned long base =
  1668. (unsigned long)hose->io_base_virt - _IO_BASE;
  1669. return base + (address - hose->io_base_phys);
  1670. }
  1671. }
  1672. return (unsigned int)-1;
  1673. }
  1674. EXPORT_SYMBOL(pci_address_to_pio);
  1675. /*
  1676. * Null PCI config access functions, for the case when we can't
  1677. * find a hose.
  1678. */
  1679. #define NULL_PCI_OP(rw, size, type) \
  1680. static int \
  1681. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  1682. { \
  1683. return PCIBIOS_DEVICE_NOT_FOUND; \
  1684. }
  1685. static int
  1686. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1687. int len, u32 *val)
  1688. {
  1689. return PCIBIOS_DEVICE_NOT_FOUND;
  1690. }
  1691. static int
  1692. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  1693. int len, u32 val)
  1694. {
  1695. return PCIBIOS_DEVICE_NOT_FOUND;
  1696. }
  1697. static struct pci_ops null_pci_ops =
  1698. {
  1699. null_read_config,
  1700. null_write_config
  1701. };
  1702. /*
  1703. * These functions are used early on before PCI scanning is done
  1704. * and all of the pci_dev and pci_bus structures have been created.
  1705. */
  1706. static struct pci_bus *
  1707. fake_pci_bus(struct pci_controller *hose, int busnr)
  1708. {
  1709. static struct pci_bus bus;
  1710. if (hose == 0) {
  1711. hose = pci_bus_to_hose(busnr);
  1712. if (hose == 0)
  1713. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  1714. }
  1715. bus.number = busnr;
  1716. bus.sysdata = hose;
  1717. bus.ops = hose? hose->ops: &null_pci_ops;
  1718. return &bus;
  1719. }
  1720. #define EARLY_PCI_OP(rw, size, type) \
  1721. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  1722. int devfn, int offset, type value) \
  1723. { \
  1724. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  1725. devfn, offset, value); \
  1726. }
  1727. EARLY_PCI_OP(read, byte, u8 *)
  1728. EARLY_PCI_OP(read, word, u16 *)
  1729. EARLY_PCI_OP(read, dword, u32 *)
  1730. EARLY_PCI_OP(write, byte, u8)
  1731. EARLY_PCI_OP(write, word, u16)
  1732. EARLY_PCI_OP(write, dword, u32)