pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  3. *
  4. * iSeries specific routines for PCI.
  5. *
  6. * Based on code from pci.c and iSeries_pci.c 32bit
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/string.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/prom.h>
  31. #include <asm/machdep.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/iommu.h>
  34. #include <asm/abs_addr.h>
  35. #include <asm/firmware.h>
  36. #include <asm/iseries/hv_call_xm.h>
  37. #include <asm/iseries/mf.h>
  38. #include <asm/iseries/iommu.h>
  39. #include <asm/ppc-pci.h>
  40. #include "irq.h"
  41. #include "pci.h"
  42. #include "call_pci.h"
  43. /*
  44. * Forward declares of prototypes.
  45. */
  46. static struct device_node *find_Device_Node(int bus, int devfn);
  47. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  48. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  49. static struct pci_ops iSeries_pci_ops;
  50. /*
  51. * Table defines
  52. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  53. */
  54. #define IOMM_TABLE_MAX_ENTRIES 1024
  55. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  56. #define BASE_IO_MEMORY 0xE000000000000000UL
  57. static unsigned long max_io_memory = BASE_IO_MEMORY;
  58. static long current_iomm_table_entry;
  59. /*
  60. * Lookup Tables.
  61. */
  62. static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
  63. static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
  64. static const char pci_io_text[] = "iSeries PCI I/O";
  65. static DEFINE_SPINLOCK(iomm_table_lock);
  66. /*
  67. * iomm_table_allocate_entry
  68. *
  69. * Adds pci_dev entry in address translation table
  70. *
  71. * - Allocates the number of entries required in table base on BAR
  72. * size.
  73. * - Allocates starting at BASE_IO_MEMORY and increases.
  74. * - The size is round up to be a multiple of entry size.
  75. * - CurrentIndex is incremented to keep track of the last entry.
  76. * - Builds the resource entry for allocated BARs.
  77. */
  78. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  79. {
  80. struct resource *bar_res = &dev->resource[bar_num];
  81. long bar_size = pci_resource_len(dev, bar_num);
  82. /*
  83. * No space to allocate, quick exit, skip Allocation.
  84. */
  85. if (bar_size == 0)
  86. return;
  87. /*
  88. * Set Resource values.
  89. */
  90. spin_lock(&iomm_table_lock);
  91. bar_res->name = pci_io_text;
  92. bar_res->start = BASE_IO_MEMORY +
  93. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  94. bar_res->end = bar_res->start + bar_size - 1;
  95. /*
  96. * Allocate the number of table entries needed for BAR.
  97. */
  98. while (bar_size > 0 ) {
  99. iomm_table[current_iomm_table_entry] = dev->sysdata;
  100. iobar_table[current_iomm_table_entry] = bar_num;
  101. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  102. ++current_iomm_table_entry;
  103. }
  104. max_io_memory = BASE_IO_MEMORY +
  105. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  106. spin_unlock(&iomm_table_lock);
  107. }
  108. /*
  109. * allocate_device_bars
  110. *
  111. * - Allocates ALL pci_dev BAR's and updates the resources with the
  112. * BAR value. BARS with zero length will have the resources
  113. * The HvCallPci_getBarParms is used to get the size of the BAR
  114. * space. It calls iomm_table_allocate_entry to allocate
  115. * each entry.
  116. * - Loops through The Bar resources(0 - 5) including the ROM
  117. * is resource(6).
  118. */
  119. static void allocate_device_bars(struct pci_dev *dev)
  120. {
  121. int bar_num;
  122. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
  123. iomm_table_allocate_entry(dev, bar_num);
  124. }
  125. /*
  126. * Log error information to system console.
  127. * Filter out the device not there errors.
  128. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  129. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  130. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  131. */
  132. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  133. int AgentId, int HvRc)
  134. {
  135. if (HvRc == 0x0302)
  136. return;
  137. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  138. Error_Text, Bus, SubBus, AgentId, HvRc);
  139. }
  140. /*
  141. * iSeries_pci_final_fixup(void)
  142. */
  143. void __init iSeries_pci_final_fixup(void)
  144. {
  145. struct pci_dev *pdev = NULL;
  146. struct device_node *node;
  147. int DeviceCount = 0;
  148. /* Fix up at the device node and pci_dev relationship */
  149. mf_display_src(0xC9000100);
  150. printk("pcibios_final_fixup\n");
  151. for_each_pci_dev(pdev) {
  152. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  153. printk("pci dev %p (%x.%x), node %p\n", pdev,
  154. pdev->bus->number, pdev->devfn, node);
  155. if (node != NULL) {
  156. struct pci_dn *pdn = PCI_DN(node);
  157. const u32 *agent;
  158. agent = of_get_property(node, "linux,agent-id", NULL);
  159. if ((pdn != NULL) && (agent != NULL)) {
  160. u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
  161. pdn->bussubno);
  162. int err;
  163. err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
  164. *agent, irq);
  165. if (err)
  166. pci_Log_Error("Connect Bus Unit",
  167. pdn->busno, pdn->bussubno, *agent, err);
  168. else {
  169. err = HvCallPci_configStore8(pdn->busno, pdn->bussubno,
  170. *agent,
  171. PCI_INTERRUPT_LINE,
  172. irq);
  173. if (err)
  174. pci_Log_Error("PciCfgStore Irq Failed!",
  175. pdn->busno, pdn->bussubno, *agent, err);
  176. }
  177. if (!err)
  178. pdev->irq = irq;
  179. }
  180. ++DeviceCount;
  181. pdev->sysdata = (void *)node;
  182. PCI_DN(node)->pcidev = pdev;
  183. allocate_device_bars(pdev);
  184. iSeries_Device_Information(pdev, DeviceCount);
  185. iommu_devnode_init_iSeries(pdev, node);
  186. } else
  187. printk("PCI: Device Tree not found for 0x%016lX\n",
  188. (unsigned long)pdev);
  189. }
  190. iSeries_activate_IRQs();
  191. mf_display_src(0xC9000200);
  192. }
  193. /*
  194. * Look down the chain to find the matching Device Device
  195. */
  196. static struct device_node *find_Device_Node(int bus, int devfn)
  197. {
  198. struct device_node *node;
  199. for (node = NULL; (node = of_find_all_nodes(node)); ) {
  200. struct pci_dn *pdn = PCI_DN(node);
  201. if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
  202. return node;
  203. }
  204. return NULL;
  205. }
  206. #if 0
  207. /*
  208. * Returns the device node for the passed pci_dev
  209. * Sanity Check Node PciDev to passed pci_dev
  210. * If none is found, returns a NULL which the client must handle.
  211. */
  212. static struct device_node *get_Device_Node(struct pci_dev *pdev)
  213. {
  214. struct device_node *node;
  215. node = pdev->sysdata;
  216. if (node == NULL || PCI_DN(node)->pcidev != pdev)
  217. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  218. return node;
  219. }
  220. #endif
  221. /*
  222. * Config space read and write functions.
  223. * For now at least, we look for the device node for the bus and devfn
  224. * that we are asked to access. It may be possible to translate the devfn
  225. * to a subbus and deviceid more directly.
  226. */
  227. static u64 hv_cfg_read_func[4] = {
  228. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  229. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  230. };
  231. static u64 hv_cfg_write_func[4] = {
  232. HvCallPciConfigStore8, HvCallPciConfigStore16,
  233. HvCallPciConfigStore32, HvCallPciConfigStore32
  234. };
  235. /*
  236. * Read PCI config space
  237. */
  238. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  239. int offset, int size, u32 *val)
  240. {
  241. struct device_node *node = find_Device_Node(bus->number, devfn);
  242. u64 fn;
  243. struct HvCallPci_LoadReturn ret;
  244. if (node == NULL)
  245. return PCIBIOS_DEVICE_NOT_FOUND;
  246. if (offset > 255) {
  247. *val = ~0;
  248. return PCIBIOS_BAD_REGISTER_NUMBER;
  249. }
  250. fn = hv_cfg_read_func[(size - 1) & 3];
  251. HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
  252. if (ret.rc != 0) {
  253. *val = ~0;
  254. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  255. }
  256. *val = ret.value;
  257. return 0;
  258. }
  259. /*
  260. * Write PCI config space
  261. */
  262. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  263. int offset, int size, u32 val)
  264. {
  265. struct device_node *node = find_Device_Node(bus->number, devfn);
  266. u64 fn;
  267. u64 ret;
  268. if (node == NULL)
  269. return PCIBIOS_DEVICE_NOT_FOUND;
  270. if (offset > 255)
  271. return PCIBIOS_BAD_REGISTER_NUMBER;
  272. fn = hv_cfg_write_func[(size - 1) & 3];
  273. ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
  274. if (ret != 0)
  275. return PCIBIOS_DEVICE_NOT_FOUND;
  276. return 0;
  277. }
  278. static struct pci_ops iSeries_pci_ops = {
  279. .read = iSeries_pci_read_config,
  280. .write = iSeries_pci_write_config
  281. };
  282. /*
  283. * Check Return Code
  284. * -> On Failure, print and log information.
  285. * Increment Retry Count, if exceeds max, panic partition.
  286. *
  287. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  288. * PCI: Device 23.90 ReadL Retry( 1)
  289. * PCI: Device 23.90 ReadL Retry Successful(1)
  290. */
  291. static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
  292. int *retry, u64 ret)
  293. {
  294. if (ret != 0) {
  295. struct pci_dn *pdn = PCI_DN(DevNode);
  296. (*retry)++;
  297. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  298. TextHdr, pdn->busno, pdn->devfn,
  299. *retry, (int)ret);
  300. /*
  301. * Bump the retry and check for retry count exceeded.
  302. * If, Exceeded, panic the system.
  303. */
  304. if (((*retry) > Pci_Retry_Max) &&
  305. (Pci_Error_Flag > 0)) {
  306. mf_display_src(0xB6000103);
  307. panic_timeout = 0;
  308. panic("PCI: Hardware I/O Error, SRC B6000103, "
  309. "Automatic Reboot Disabled.\n");
  310. }
  311. return -1; /* Retry Try */
  312. }
  313. return 0;
  314. }
  315. /*
  316. * Translate the I/O Address into a device node, bar, and bar offset.
  317. * Note: Make sure the passed variable end up on the stack to avoid
  318. * the exposure of being device global.
  319. */
  320. static inline struct device_node *xlate_iomm_address(
  321. const volatile void __iomem *IoAddress,
  322. u64 *dsaptr, u64 *BarOffsetPtr)
  323. {
  324. unsigned long OrigIoAddr;
  325. unsigned long BaseIoAddr;
  326. unsigned long TableIndex;
  327. struct device_node *DevNode;
  328. OrigIoAddr = (unsigned long __force)IoAddress;
  329. if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
  330. return NULL;
  331. BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
  332. TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
  333. DevNode = iomm_table[TableIndex];
  334. if (DevNode != NULL) {
  335. int barnum = iobar_table[TableIndex];
  336. *dsaptr = iseries_ds_addr(DevNode) | (barnum << 24);
  337. *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
  338. } else
  339. panic("PCI: Invalid PCI IoAddress detected!\n");
  340. return DevNode;
  341. }
  342. /*
  343. * Read MM I/O Instructions for the iSeries
  344. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  345. * else, data is returned in Big Endian format.
  346. */
  347. static u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
  348. {
  349. u64 BarOffset;
  350. u64 dsa;
  351. int retry = 0;
  352. struct HvCallPci_LoadReturn ret;
  353. struct device_node *DevNode =
  354. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  355. if (DevNode == NULL) {
  356. static unsigned long last_jiffies;
  357. static int num_printed;
  358. if ((jiffies - last_jiffies) > 60 * HZ) {
  359. last_jiffies = jiffies;
  360. num_printed = 0;
  361. }
  362. if (num_printed++ < 10)
  363. printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n",
  364. IoAddress);
  365. return 0xff;
  366. }
  367. do {
  368. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
  369. } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
  370. return ret.value;
  371. }
  372. static u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
  373. {
  374. u64 BarOffset;
  375. u64 dsa;
  376. int retry = 0;
  377. struct HvCallPci_LoadReturn ret;
  378. struct device_node *DevNode =
  379. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  380. if (DevNode == NULL) {
  381. static unsigned long last_jiffies;
  382. static int num_printed;
  383. if ((jiffies - last_jiffies) > 60 * HZ) {
  384. last_jiffies = jiffies;
  385. num_printed = 0;
  386. }
  387. if (num_printed++ < 10)
  388. printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n",
  389. IoAddress);
  390. return 0xffff;
  391. }
  392. do {
  393. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  394. BarOffset, 0);
  395. } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
  396. return ret.value;
  397. }
  398. static u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
  399. {
  400. u64 BarOffset;
  401. u64 dsa;
  402. int retry = 0;
  403. struct HvCallPci_LoadReturn ret;
  404. struct device_node *DevNode =
  405. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  406. if (DevNode == NULL) {
  407. static unsigned long last_jiffies;
  408. static int num_printed;
  409. if ((jiffies - last_jiffies) > 60 * HZ) {
  410. last_jiffies = jiffies;
  411. num_printed = 0;
  412. }
  413. if (num_printed++ < 10)
  414. printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n",
  415. IoAddress);
  416. return 0xffffffff;
  417. }
  418. do {
  419. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  420. BarOffset, 0);
  421. } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
  422. return ret.value;
  423. }
  424. /*
  425. * Write MM I/O Instructions for the iSeries
  426. *
  427. */
  428. static void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
  429. {
  430. u64 BarOffset;
  431. u64 dsa;
  432. int retry = 0;
  433. u64 rc;
  434. struct device_node *DevNode =
  435. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  436. if (DevNode == NULL) {
  437. static unsigned long last_jiffies;
  438. static int num_printed;
  439. if ((jiffies - last_jiffies) > 60 * HZ) {
  440. last_jiffies = jiffies;
  441. num_printed = 0;
  442. }
  443. if (num_printed++ < 10)
  444. printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
  445. return;
  446. }
  447. do {
  448. rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
  449. } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
  450. }
  451. static void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
  452. {
  453. u64 BarOffset;
  454. u64 dsa;
  455. int retry = 0;
  456. u64 rc;
  457. struct device_node *DevNode =
  458. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  459. if (DevNode == NULL) {
  460. static unsigned long last_jiffies;
  461. static int num_printed;
  462. if ((jiffies - last_jiffies) > 60 * HZ) {
  463. last_jiffies = jiffies;
  464. num_printed = 0;
  465. }
  466. if (num_printed++ < 10)
  467. printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n",
  468. IoAddress);
  469. return;
  470. }
  471. do {
  472. rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, data, 0);
  473. } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
  474. }
  475. static void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
  476. {
  477. u64 BarOffset;
  478. u64 dsa;
  479. int retry = 0;
  480. u64 rc;
  481. struct device_node *DevNode =
  482. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  483. if (DevNode == NULL) {
  484. static unsigned long last_jiffies;
  485. static int num_printed;
  486. if ((jiffies - last_jiffies) > 60 * HZ) {
  487. last_jiffies = jiffies;
  488. num_printed = 0;
  489. }
  490. if (num_printed++ < 10)
  491. printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n",
  492. IoAddress);
  493. return;
  494. }
  495. do {
  496. rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, data, 0);
  497. } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
  498. }
  499. static u8 iseries_readb(const volatile void __iomem *addr)
  500. {
  501. return iSeries_Read_Byte(addr);
  502. }
  503. static u16 iseries_readw(const volatile void __iomem *addr)
  504. {
  505. return le16_to_cpu(iSeries_Read_Word(addr));
  506. }
  507. static u32 iseries_readl(const volatile void __iomem *addr)
  508. {
  509. return le32_to_cpu(iSeries_Read_Long(addr));
  510. }
  511. static u16 iseries_readw_be(const volatile void __iomem *addr)
  512. {
  513. return iSeries_Read_Word(addr);
  514. }
  515. static u32 iseries_readl_be(const volatile void __iomem *addr)
  516. {
  517. return iSeries_Read_Long(addr);
  518. }
  519. static void iseries_writeb(u8 data, volatile void __iomem *addr)
  520. {
  521. iSeries_Write_Byte(data, addr);
  522. }
  523. static void iseries_writew(u16 data, volatile void __iomem *addr)
  524. {
  525. iSeries_Write_Word(cpu_to_le16(data), addr);
  526. }
  527. static void iseries_writel(u32 data, volatile void __iomem *addr)
  528. {
  529. iSeries_Write_Long(cpu_to_le32(data), addr);
  530. }
  531. static void iseries_writew_be(u16 data, volatile void __iomem *addr)
  532. {
  533. iSeries_Write_Word(data, addr);
  534. }
  535. static void iseries_writel_be(u32 data, volatile void __iomem *addr)
  536. {
  537. iSeries_Write_Long(data, addr);
  538. }
  539. static void iseries_readsb(const volatile void __iomem *addr, void *buf,
  540. unsigned long count)
  541. {
  542. u8 *dst = buf;
  543. while(count-- > 0)
  544. *(dst++) = iSeries_Read_Byte(addr);
  545. }
  546. static void iseries_readsw(const volatile void __iomem *addr, void *buf,
  547. unsigned long count)
  548. {
  549. u16 *dst = buf;
  550. while(count-- > 0)
  551. *(dst++) = iSeries_Read_Word(addr);
  552. }
  553. static void iseries_readsl(const volatile void __iomem *addr, void *buf,
  554. unsigned long count)
  555. {
  556. u32 *dst = buf;
  557. while(count-- > 0)
  558. *(dst++) = iSeries_Read_Long(addr);
  559. }
  560. static void iseries_writesb(volatile void __iomem *addr, const void *buf,
  561. unsigned long count)
  562. {
  563. const u8 *src = buf;
  564. while(count-- > 0)
  565. iSeries_Write_Byte(*(src++), addr);
  566. }
  567. static void iseries_writesw(volatile void __iomem *addr, const void *buf,
  568. unsigned long count)
  569. {
  570. const u16 *src = buf;
  571. while(count-- > 0)
  572. iSeries_Write_Word(*(src++), addr);
  573. }
  574. static void iseries_writesl(volatile void __iomem *addr, const void *buf,
  575. unsigned long count)
  576. {
  577. const u32 *src = buf;
  578. while(count-- > 0)
  579. iSeries_Write_Long(*(src++), addr);
  580. }
  581. static void iseries_memset_io(volatile void __iomem *addr, int c,
  582. unsigned long n)
  583. {
  584. volatile char __iomem *d = addr;
  585. while (n-- > 0)
  586. iSeries_Write_Byte(c, d++);
  587. }
  588. static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
  589. unsigned long n)
  590. {
  591. char *d = dest;
  592. const volatile char __iomem *s = src;
  593. while (n-- > 0)
  594. *d++ = iSeries_Read_Byte(s++);
  595. }
  596. static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
  597. unsigned long n)
  598. {
  599. const char *s = src;
  600. volatile char __iomem *d = dest;
  601. while (n-- > 0)
  602. iSeries_Write_Byte(*s++, d++);
  603. }
  604. /* We only set MMIO ops. The default PIO ops will be default
  605. * to the MMIO ops + pci_io_base which is 0 on iSeries as
  606. * expected so both should work.
  607. *
  608. * Note that we don't implement the readq/writeq versions as
  609. * I don't know of an HV call for doing so. Thus, the default
  610. * operation will be used instead, which will fault a the value
  611. * return by iSeries for MMIO addresses always hits a non mapped
  612. * area. This is as good as the BUG() we used to have there.
  613. */
  614. static struct ppc_pci_io __initdata iseries_pci_io = {
  615. .readb = iseries_readb,
  616. .readw = iseries_readw,
  617. .readl = iseries_readl,
  618. .readw_be = iseries_readw_be,
  619. .readl_be = iseries_readl_be,
  620. .writeb = iseries_writeb,
  621. .writew = iseries_writew,
  622. .writel = iseries_writel,
  623. .writew_be = iseries_writew_be,
  624. .writel_be = iseries_writel_be,
  625. .readsb = iseries_readsb,
  626. .readsw = iseries_readsw,
  627. .readsl = iseries_readsl,
  628. .writesb = iseries_writesb,
  629. .writesw = iseries_writesw,
  630. .writesl = iseries_writesl,
  631. .memset_io = iseries_memset_io,
  632. .memcpy_fromio = iseries_memcpy_fromio,
  633. .memcpy_toio = iseries_memcpy_toio,
  634. };
  635. /*
  636. * iSeries_pcibios_init
  637. *
  638. * Description:
  639. * This function checks for all possible system PCI host bridges that connect
  640. * PCI buses. The system hypervisor is queried as to the guest partition
  641. * ownership status. A pci_controller is built for any bus which is partially
  642. * owned or fully owned by this guest partition.
  643. */
  644. void __init iSeries_pcibios_init(void)
  645. {
  646. struct pci_controller *phb;
  647. struct device_node *root = of_find_node_by_path("/");
  648. struct device_node *node = NULL;
  649. /* Install IO hooks */
  650. ppc_pci_io = iseries_pci_io;
  651. /* iSeries has no IO space in the common sense, it needs to set
  652. * the IO base to 0
  653. */
  654. pci_io_base = 0;
  655. if (root == NULL) {
  656. printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
  657. "of device tree\n");
  658. return;
  659. }
  660. while ((node = of_get_next_child(root, node)) != NULL) {
  661. HvBusNumber bus;
  662. const u32 *busp;
  663. if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
  664. continue;
  665. busp = of_get_property(node, "bus-range", NULL);
  666. if (busp == NULL)
  667. continue;
  668. bus = *busp;
  669. printk("bus %d appears to exist\n", bus);
  670. phb = pcibios_alloc_controller(node);
  671. if (phb == NULL)
  672. continue;
  673. phb->pci_mem_offset = bus;
  674. phb->first_busno = bus;
  675. phb->last_busno = bus;
  676. phb->ops = &iSeries_pci_ops;
  677. }
  678. of_node_put(root);
  679. pci_devs_phb_init();
  680. }