rtas_pci.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  3. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  4. *
  5. * RTAS specific routines for PCI.
  6. *
  7. * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/threads.h>
  25. #include <linux/pci.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <linux/bootmem.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/irq.h>
  32. #include <asm/prom.h>
  33. #include <asm/machdep.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/iommu.h>
  36. #include <asm/rtas.h>
  37. #include <asm/mpic.h>
  38. #include <asm/ppc-pci.h>
  39. /* RTAS tokens */
  40. static int read_pci_config;
  41. static int write_pci_config;
  42. static int ibm_read_pci_config;
  43. static int ibm_write_pci_config;
  44. static inline int config_access_valid(struct pci_dn *dn, int where)
  45. {
  46. if (where < 256)
  47. return 1;
  48. if (where < 4096 && dn->pci_ext_config_space)
  49. return 1;
  50. return 0;
  51. }
  52. static int of_device_available(struct device_node * dn)
  53. {
  54. char * status;
  55. status = get_property(dn, "status", NULL);
  56. if (!status)
  57. return 1;
  58. if (!strcmp(status, "okay"))
  59. return 1;
  60. return 0;
  61. }
  62. int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
  63. {
  64. int returnval = -1;
  65. unsigned long buid, addr;
  66. int ret;
  67. if (!pdn)
  68. return PCIBIOS_DEVICE_NOT_FOUND;
  69. if (!config_access_valid(pdn, where))
  70. return PCIBIOS_BAD_REGISTER_NUMBER;
  71. addr = ((where & 0xf00) << 20) | (pdn->busno << 16) |
  72. (pdn->devfn << 8) | (where & 0xff);
  73. buid = pdn->phb->buid;
  74. if (buid) {
  75. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  76. addr, BUID_HI(buid), BUID_LO(buid), size);
  77. } else {
  78. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  79. }
  80. *val = returnval;
  81. if (ret)
  82. return PCIBIOS_DEVICE_NOT_FOUND;
  83. if (returnval == EEH_IO_ERROR_VALUE(size) &&
  84. eeh_dn_check_failure (pdn->node, NULL))
  85. return PCIBIOS_DEVICE_NOT_FOUND;
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. static int rtas_pci_read_config(struct pci_bus *bus,
  89. unsigned int devfn,
  90. int where, int size, u32 *val)
  91. {
  92. struct device_node *busdn, *dn;
  93. if (bus->self)
  94. busdn = pci_device_to_OF_node(bus->self);
  95. else
  96. busdn = bus->sysdata; /* must be a phb */
  97. /* Search only direct children of the bus */
  98. for (dn = busdn->child; dn; dn = dn->sibling) {
  99. struct pci_dn *pdn = PCI_DN(dn);
  100. if (pdn && pdn->devfn == devfn
  101. && of_device_available(dn))
  102. return rtas_read_config(pdn, where, size, val);
  103. }
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. }
  106. int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
  107. {
  108. unsigned long buid, addr;
  109. int ret;
  110. if (!pdn)
  111. return PCIBIOS_DEVICE_NOT_FOUND;
  112. if (!config_access_valid(pdn, where))
  113. return PCIBIOS_BAD_REGISTER_NUMBER;
  114. addr = ((where & 0xf00) << 20) | (pdn->busno << 16) |
  115. (pdn->devfn << 8) | (where & 0xff);
  116. buid = pdn->phb->buid;
  117. if (buid) {
  118. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
  119. BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
  120. } else {
  121. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  122. }
  123. if (ret)
  124. return PCIBIOS_DEVICE_NOT_FOUND;
  125. return PCIBIOS_SUCCESSFUL;
  126. }
  127. static int rtas_pci_write_config(struct pci_bus *bus,
  128. unsigned int devfn,
  129. int where, int size, u32 val)
  130. {
  131. struct device_node *busdn, *dn;
  132. if (bus->self)
  133. busdn = pci_device_to_OF_node(bus->self);
  134. else
  135. busdn = bus->sysdata; /* must be a phb */
  136. /* Search only direct children of the bus */
  137. for (dn = busdn->child; dn; dn = dn->sibling) {
  138. struct pci_dn *pdn = PCI_DN(dn);
  139. if (pdn && pdn->devfn == devfn
  140. && of_device_available(dn))
  141. return rtas_write_config(pdn, where, size, val);
  142. }
  143. return PCIBIOS_DEVICE_NOT_FOUND;
  144. }
  145. struct pci_ops rtas_pci_ops = {
  146. rtas_pci_read_config,
  147. rtas_pci_write_config
  148. };
  149. int is_python(struct device_node *dev)
  150. {
  151. char *model = (char *)get_property(dev, "model", NULL);
  152. if (model && strstr(model, "Python"))
  153. return 1;
  154. return 0;
  155. }
  156. static void python_countermeasures(struct device_node *dev)
  157. {
  158. struct resource registers;
  159. void __iomem *chip_regs;
  160. volatile u32 val;
  161. if (of_address_to_resource(dev, 0, &registers)) {
  162. printk(KERN_ERR "Can't get address for Python workarounds !\n");
  163. return;
  164. }
  165. /* Python's register file is 1 MB in size. */
  166. chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
  167. /*
  168. * Firmware doesn't always clear this bit which is critical
  169. * for good performance - Anton
  170. */
  171. #define PRG_CL_RESET_VALID 0x00010000
  172. val = in_be32(chip_regs + 0xf6030);
  173. if (val & PRG_CL_RESET_VALID) {
  174. printk(KERN_INFO "Python workaround: ");
  175. val &= ~PRG_CL_RESET_VALID;
  176. out_be32(chip_regs + 0xf6030, val);
  177. /*
  178. * We must read it back for changes to
  179. * take effect
  180. */
  181. val = in_be32(chip_regs + 0xf6030);
  182. printk("reg0: %x\n", val);
  183. }
  184. iounmap(chip_regs);
  185. }
  186. void __init init_pci_config_tokens (void)
  187. {
  188. read_pci_config = rtas_token("read-pci-config");
  189. write_pci_config = rtas_token("write-pci-config");
  190. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  191. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  192. }
  193. unsigned long __devinit get_phb_buid (struct device_node *phb)
  194. {
  195. int addr_cells;
  196. unsigned int *buid_vals;
  197. unsigned int len;
  198. unsigned long buid;
  199. if (ibm_read_pci_config == -1) return 0;
  200. /* PHB's will always be children of the root node,
  201. * or so it is promised by the current firmware. */
  202. if (phb->parent == NULL)
  203. return 0;
  204. if (phb->parent->parent)
  205. return 0;
  206. buid_vals = (unsigned int *) get_property(phb, "reg", &len);
  207. if (buid_vals == NULL)
  208. return 0;
  209. addr_cells = prom_n_addr_cells(phb);
  210. if (addr_cells == 1) {
  211. buid = (unsigned long) buid_vals[0];
  212. } else {
  213. buid = (((unsigned long)buid_vals[0]) << 32UL) |
  214. (((unsigned long)buid_vals[1]) & 0xffffffff);
  215. }
  216. return buid;
  217. }
  218. static int phb_set_bus_ranges(struct device_node *dev,
  219. struct pci_controller *phb)
  220. {
  221. int *bus_range;
  222. unsigned int len;
  223. bus_range = (int *) get_property(dev, "bus-range", &len);
  224. if (bus_range == NULL || len < 2 * sizeof(int)) {
  225. return 1;
  226. }
  227. phb->first_busno = bus_range[0];
  228. phb->last_busno = bus_range[1];
  229. return 0;
  230. }
  231. int __devinit setup_phb(struct device_node *dev, struct pci_controller *phb)
  232. {
  233. if (is_python(dev))
  234. python_countermeasures(dev);
  235. if (phb_set_bus_ranges(dev, phb))
  236. return 1;
  237. phb->ops = &rtas_pci_ops;
  238. phb->buid = get_phb_buid(dev);
  239. return 0;
  240. }
  241. unsigned long __init find_and_init_phbs(void)
  242. {
  243. struct device_node *node;
  244. struct pci_controller *phb;
  245. unsigned int index;
  246. unsigned int root_size_cells = 0;
  247. unsigned int *opprop = NULL;
  248. struct device_node *root = of_find_node_by_path("/");
  249. if (ppc64_interrupt_controller == IC_OPEN_PIC) {
  250. opprop = (unsigned int *)get_property(root,
  251. "platform-open-pic", NULL);
  252. }
  253. root_size_cells = prom_n_size_cells(root);
  254. index = 0;
  255. for (node = of_get_next_child(root, NULL);
  256. node != NULL;
  257. node = of_get_next_child(root, node)) {
  258. if (node->type == NULL || strcmp(node->type, "pci") != 0)
  259. continue;
  260. phb = pcibios_alloc_controller(node);
  261. if (!phb)
  262. continue;
  263. setup_phb(node, phb);
  264. pci_process_bridge_OF_ranges(phb, node, 0);
  265. pci_setup_phb_io(phb, index == 0);
  266. #ifdef CONFIG_PPC_PSERIES
  267. /* XXX This code need serious fixing ... --BenH */
  268. if (ppc64_interrupt_controller == IC_OPEN_PIC && pSeries_mpic) {
  269. int addr = root_size_cells * (index + 2) - 1;
  270. mpic_assign_isu(pSeries_mpic, index, opprop[addr]);
  271. }
  272. #endif
  273. index++;
  274. }
  275. of_node_put(root);
  276. pci_devs_phb_init();
  277. /*
  278. * pci_probe_only and pci_assign_all_buses can be set via properties
  279. * in chosen.
  280. */
  281. if (of_chosen) {
  282. int *prop;
  283. prop = (int *)get_property(of_chosen, "linux,pci-probe-only",
  284. NULL);
  285. if (prop)
  286. pci_probe_only = *prop;
  287. prop = (int *)get_property(of_chosen,
  288. "linux,pci-assign-all-buses", NULL);
  289. if (prop)
  290. pci_assign_all_buses = *prop;
  291. }
  292. return 0;
  293. }
  294. /* RPA-specific bits for removing PHBs */
  295. int pcibios_remove_root_bus(struct pci_controller *phb)
  296. {
  297. struct pci_bus *b = phb->bus;
  298. struct resource *res;
  299. int rc, i;
  300. res = b->resource[0];
  301. if (!res->flags) {
  302. printk(KERN_ERR "%s: no IO resource for PHB %s\n", __FUNCTION__,
  303. b->name);
  304. return 1;
  305. }
  306. rc = unmap_bus_range(b);
  307. if (rc) {
  308. printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
  309. __FUNCTION__, b->name);
  310. return 1;
  311. }
  312. if (release_resource(res)) {
  313. printk(KERN_ERR "%s: failed to release IO on bus %s\n",
  314. __FUNCTION__, b->name);
  315. return 1;
  316. }
  317. for (i = 1; i < 3; ++i) {
  318. res = b->resource[i];
  319. if (!res->flags && i == 0) {
  320. printk(KERN_ERR "%s: no MEM resource for PHB %s\n",
  321. __FUNCTION__, b->name);
  322. return 1;
  323. }
  324. if (res->flags && release_resource(res)) {
  325. printk(KERN_ERR
  326. "%s: failed to release IO %d on bus %s\n",
  327. __FUNCTION__, i, b->name);
  328. return 1;
  329. }
  330. }
  331. list_del(&phb->list_node);
  332. pcibios_free_controller(phb);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(pcibios_remove_root_bus);