rtas_pci.c 11 KB

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