pci.c 32 KB

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