pci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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/ide.h>
  28. #include <linux/pci.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/prom.h>
  32. #include <asm/machdep.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/ppcdebug.h>
  35. #include <asm/iommu.h>
  36. #include <asm/iSeries/HvCallPci.h>
  37. #include <asm/iSeries/HvCallXm.h>
  38. #include <asm/iSeries/iSeries_irq.h>
  39. #include <asm/iSeries/iSeries_pci.h>
  40. #include <asm/iSeries/mf.h>
  41. #include <asm/ppc-pci.h>
  42. extern unsigned long io_page_mask;
  43. /*
  44. * Forward declares of prototypes.
  45. */
  46. static struct device_node *find_Device_Node(int bus, int devfn);
  47. static void scan_PHB_slots(struct pci_controller *Phb);
  48. static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
  49. static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
  50. LIST_HEAD(iSeries_Global_Device_List);
  51. static int DeviceCount;
  52. /* Counters and control flags. */
  53. static long Pci_Io_Read_Count;
  54. static long Pci_Io_Write_Count;
  55. #if 0
  56. static long Pci_Cfg_Read_Count;
  57. static long Pci_Cfg_Write_Count;
  58. #endif
  59. static long Pci_Error_Count;
  60. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  61. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  62. static struct pci_ops iSeries_pci_ops;
  63. /*
  64. * Table defines
  65. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  66. */
  67. #define IOMM_TABLE_MAX_ENTRIES 1024
  68. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  69. #define BASE_IO_MEMORY 0xE000000000000000UL
  70. static unsigned long max_io_memory = 0xE000000000000000UL;
  71. static long current_iomm_table_entry;
  72. /*
  73. * Lookup Tables.
  74. */
  75. static struct device_node **iomm_table;
  76. static u8 *iobar_table;
  77. /*
  78. * Static and Global variables
  79. */
  80. static char *pci_io_text = "iSeries PCI I/O";
  81. static DEFINE_SPINLOCK(iomm_table_lock);
  82. /*
  83. * iomm_table_initialize
  84. *
  85. * Allocates and initalizes the Address Translation Table and Bar
  86. * Tables to get them ready for use. Must be called before any
  87. * I/O space is handed out to the device BARs.
  88. */
  89. static void iomm_table_initialize(void)
  90. {
  91. spin_lock(&iomm_table_lock);
  92. iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
  93. GFP_KERNEL);
  94. iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
  95. GFP_KERNEL);
  96. spin_unlock(&iomm_table_lock);
  97. if ((iomm_table == NULL) || (iobar_table == NULL))
  98. panic("PCI: I/O tables allocation failed.\n");
  99. }
  100. /*
  101. * iomm_table_allocate_entry
  102. *
  103. * Adds pci_dev entry in address translation table
  104. *
  105. * - Allocates the number of entries required in table base on BAR
  106. * size.
  107. * - Allocates starting at BASE_IO_MEMORY and increases.
  108. * - The size is round up to be a multiple of entry size.
  109. * - CurrentIndex is incremented to keep track of the last entry.
  110. * - Builds the resource entry for allocated BARs.
  111. */
  112. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  113. {
  114. struct resource *bar_res = &dev->resource[bar_num];
  115. long bar_size = pci_resource_len(dev, bar_num);
  116. /*
  117. * No space to allocate, quick exit, skip Allocation.
  118. */
  119. if (bar_size == 0)
  120. return;
  121. /*
  122. * Set Resource values.
  123. */
  124. spin_lock(&iomm_table_lock);
  125. bar_res->name = pci_io_text;
  126. bar_res->start =
  127. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  128. bar_res->start += BASE_IO_MEMORY;
  129. bar_res->end = bar_res->start + bar_size - 1;
  130. /*
  131. * Allocate the number of table entries needed for BAR.
  132. */
  133. while (bar_size > 0 ) {
  134. iomm_table[current_iomm_table_entry] = dev->sysdata;
  135. iobar_table[current_iomm_table_entry] = bar_num;
  136. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  137. ++current_iomm_table_entry;
  138. }
  139. max_io_memory = BASE_IO_MEMORY +
  140. (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
  141. spin_unlock(&iomm_table_lock);
  142. }
  143. /*
  144. * allocate_device_bars
  145. *
  146. * - Allocates ALL pci_dev BAR's and updates the resources with the
  147. * BAR value. BARS with zero length will have the resources
  148. * The HvCallPci_getBarParms is used to get the size of the BAR
  149. * space. It calls iomm_table_allocate_entry to allocate
  150. * each entry.
  151. * - Loops through The Bar resources(0 - 5) including the ROM
  152. * is resource(6).
  153. */
  154. static void allocate_device_bars(struct pci_dev *dev)
  155. {
  156. struct resource *bar_res;
  157. int bar_num;
  158. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
  159. bar_res = &dev->resource[bar_num];
  160. iomm_table_allocate_entry(dev, bar_num);
  161. }
  162. }
  163. /*
  164. * Log error information to system console.
  165. * Filter out the device not there errors.
  166. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  167. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  168. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  169. */
  170. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  171. int AgentId, int HvRc)
  172. {
  173. if (HvRc == 0x0302)
  174. return;
  175. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  176. Error_Text, Bus, SubBus, AgentId, HvRc);
  177. }
  178. /*
  179. * build_device_node(u16 Bus, int SubBus, u8 DevFn)
  180. */
  181. static struct device_node *build_device_node(HvBusNumber Bus,
  182. HvSubBusNumber SubBus, int AgentId, int Function)
  183. {
  184. struct device_node *node;
  185. struct pci_dn *pdn;
  186. PPCDBG(PPCDBG_BUSWALK,
  187. "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
  188. Bus, SubBus, AgentId, Function);
  189. node = kmalloc(sizeof(struct device_node), GFP_KERNEL);
  190. if (node == NULL)
  191. return NULL;
  192. memset(node, 0, sizeof(struct device_node));
  193. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  194. if (pdn == NULL) {
  195. kfree(node);
  196. return NULL;
  197. }
  198. node->data = pdn;
  199. list_add_tail(&node->Device_List, &iSeries_Global_Device_List);
  200. #if 0
  201. pdn->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
  202. #endif
  203. pdn->DsaAddr.DsaAddr = 0;
  204. pdn->DsaAddr.Dsa.busNumber = Bus;
  205. pdn->DsaAddr.Dsa.subBusNumber = SubBus;
  206. pdn->DsaAddr.Dsa.deviceId = 0x10;
  207. pdn->devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
  208. return node;
  209. }
  210. /*
  211. * unsigned long __init find_and_init_phbs(void)
  212. *
  213. * Description:
  214. * This function checks for all possible system PCI host bridges that connect
  215. * PCI buses. The system hypervisor is queried as to the guest partition
  216. * ownership status. A pci_controller is built for any bus which is partially
  217. * owned or fully owned by this guest partition.
  218. */
  219. unsigned long __init find_and_init_phbs(void)
  220. {
  221. struct pci_controller *phb;
  222. HvBusNumber bus;
  223. PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
  224. /* Check all possible buses. */
  225. for (bus = 0; bus < 256; bus++) {
  226. int ret = HvCallXm_testBus(bus);
  227. if (ret == 0) {
  228. printk("bus %d appears to exist\n", bus);
  229. phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  230. if (phb == NULL)
  231. return -ENOMEM;
  232. pci_setup_pci_controller(phb);
  233. phb->pci_mem_offset = phb->local_number = bus;
  234. phb->first_busno = bus;
  235. phb->last_busno = bus;
  236. phb->ops = &iSeries_pci_ops;
  237. PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
  238. phb, bus);
  239. /* Find and connect the devices. */
  240. scan_PHB_slots(phb);
  241. }
  242. /*
  243. * Check for Unexpected Return code, a clue that something
  244. * has gone wrong.
  245. */
  246. else if (ret != 0x0301)
  247. printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
  248. bus, ret);
  249. }
  250. return 0;
  251. }
  252. /*
  253. * iSeries_pcibios_init
  254. *
  255. * Chance to initialize and structures or variable before PCI Bus walk.
  256. */
  257. void iSeries_pcibios_init(void)
  258. {
  259. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n");
  260. iomm_table_initialize();
  261. find_and_init_phbs();
  262. io_page_mask = -1;
  263. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n");
  264. }
  265. /*
  266. * iSeries_pci_final_fixup(void)
  267. */
  268. void __init iSeries_pci_final_fixup(void)
  269. {
  270. struct pci_dev *pdev = NULL;
  271. struct device_node *node;
  272. int DeviceCount = 0;
  273. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n");
  274. /* Fix up at the device node and pci_dev relationship */
  275. mf_display_src(0xC9000100);
  276. printk("pcibios_final_fixup\n");
  277. for_each_pci_dev(pdev) {
  278. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  279. printk("pci dev %p (%x.%x), node %p\n", pdev,
  280. pdev->bus->number, pdev->devfn, node);
  281. if (node != NULL) {
  282. ++DeviceCount;
  283. pdev->sysdata = (void *)node;
  284. PCI_DN(node)->pcidev = pdev;
  285. PPCDBG(PPCDBG_BUSWALK,
  286. "pdev 0x%p <==> DevNode 0x%p\n",
  287. pdev, node);
  288. allocate_device_bars(pdev);
  289. iSeries_Device_Information(pdev, DeviceCount);
  290. iommu_devnode_init_iSeries(node);
  291. } else
  292. printk("PCI: Device Tree not found for 0x%016lX\n",
  293. (unsigned long)pdev);
  294. pdev->irq = PCI_DN(node)->Irq;
  295. }
  296. iSeries_activate_IRQs();
  297. mf_display_src(0xC9000200);
  298. }
  299. void pcibios_fixup_bus(struct pci_bus *PciBus)
  300. {
  301. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
  302. PciBus->number);
  303. }
  304. void pcibios_fixup_resources(struct pci_dev *pdev)
  305. {
  306. PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
  307. }
  308. /*
  309. * Loop through each node function to find usable EADs bridges.
  310. */
  311. static void scan_PHB_slots(struct pci_controller *Phb)
  312. {
  313. struct HvCallPci_DeviceInfo *DevInfo;
  314. HvBusNumber bus = Phb->local_number; /* System Bus */
  315. const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
  316. int HvRc = 0;
  317. int IdSel;
  318. const int MaxAgents = 8;
  319. DevInfo = (struct HvCallPci_DeviceInfo*)
  320. kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
  321. if (DevInfo == NULL)
  322. return;
  323. /*
  324. * Probe for EADs Bridges
  325. */
  326. for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
  327. HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
  328. ISERIES_HV_ADDR(DevInfo),
  329. sizeof(struct HvCallPci_DeviceInfo));
  330. if (HvRc == 0) {
  331. if (DevInfo->deviceType == HvCallPci_NodeDevice)
  332. scan_EADS_bridge(bus, SubBus, IdSel);
  333. else
  334. printk("PCI: Invalid System Configuration(0x%02X)"
  335. " for bus 0x%02x id 0x%02x.\n",
  336. DevInfo->deviceType, bus, IdSel);
  337. }
  338. else
  339. pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
  340. }
  341. kfree(DevInfo);
  342. }
  343. static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
  344. int IdSel)
  345. {
  346. struct HvCallPci_BridgeInfo *BridgeInfo;
  347. HvAgentId AgentId;
  348. int Function;
  349. int HvRc;
  350. BridgeInfo = (struct HvCallPci_BridgeInfo *)
  351. kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
  352. if (BridgeInfo == NULL)
  353. return;
  354. /* Note: hvSubBus and irq is always be 0 at this level! */
  355. for (Function = 0; Function < 8; ++Function) {
  356. AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  357. HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
  358. if (HvRc == 0) {
  359. printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
  360. bus, IdSel, Function, AgentId);
  361. /* Connect EADs: 0x18.00.12 = 0x00 */
  362. PPCDBG(PPCDBG_BUSWALK,
  363. "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
  364. bus, SubBus, AgentId);
  365. HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
  366. ISERIES_HV_ADDR(BridgeInfo),
  367. sizeof(struct HvCallPci_BridgeInfo));
  368. if (HvRc == 0) {
  369. printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
  370. BridgeInfo->busUnitInfo.deviceType,
  371. BridgeInfo->subBusNumber,
  372. BridgeInfo->maxAgents,
  373. BridgeInfo->maxSubBusNumber,
  374. BridgeInfo->logicalSlotNumber);
  375. PPCDBG(PPCDBG_BUSWALK,
  376. "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
  377. BridgeInfo->busUnitInfo.deviceType,
  378. BridgeInfo->subBusNumber,
  379. BridgeInfo->maxAgents,
  380. BridgeInfo->maxSubBusNumber,
  381. BridgeInfo->logicalSlotNumber);
  382. if (BridgeInfo->busUnitInfo.deviceType ==
  383. HvCallPci_BridgeDevice) {
  384. /* Scan_Bridge_Slot...: 0x18.00.12 */
  385. scan_bridge_slot(bus, BridgeInfo);
  386. } else
  387. printk("PCI: Invalid Bridge Configuration(0x%02X)",
  388. BridgeInfo->busUnitInfo.deviceType);
  389. }
  390. } else if (HvRc != 0x000B)
  391. pci_Log_Error("EADs Connect",
  392. bus, SubBus, AgentId, HvRc);
  393. }
  394. kfree(BridgeInfo);
  395. }
  396. /*
  397. * This assumes that the node slot is always on the primary bus!
  398. */
  399. static int scan_bridge_slot(HvBusNumber Bus,
  400. struct HvCallPci_BridgeInfo *BridgeInfo)
  401. {
  402. struct device_node *node;
  403. HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
  404. u16 VendorId = 0;
  405. int HvRc = 0;
  406. u8 Irq = 0;
  407. int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
  408. int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
  409. HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
  410. /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
  411. Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
  412. PPCDBG(PPCDBG_BUSWALK,
  413. "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
  414. Bus, 0, EADsIdSel, Irq);
  415. /*
  416. * Connect all functions of any device found.
  417. */
  418. for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
  419. for (Function = 0; Function < 8; ++Function) {
  420. HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  421. HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
  422. AgentId, Irq);
  423. if (HvRc != 0) {
  424. pci_Log_Error("Connect Bus Unit",
  425. Bus, SubBus, AgentId, HvRc);
  426. continue;
  427. }
  428. HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
  429. PCI_VENDOR_ID, &VendorId);
  430. if (HvRc != 0) {
  431. pci_Log_Error("Read Vendor",
  432. Bus, SubBus, AgentId, HvRc);
  433. continue;
  434. }
  435. printk("read vendor ID: %x\n", VendorId);
  436. /* FoundDevice: 0x18.28.10 = 0x12AE */
  437. PPCDBG(PPCDBG_BUSWALK,
  438. "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
  439. Bus, SubBus, AgentId, VendorId, Irq);
  440. HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
  441. PCI_INTERRUPT_LINE, Irq);
  442. if (HvRc != 0)
  443. pci_Log_Error("PciCfgStore Irq Failed!",
  444. Bus, SubBus, AgentId, HvRc);
  445. ++DeviceCount;
  446. node = build_device_node(Bus, SubBus, EADsIdSel, Function);
  447. PCI_DN(node)->Irq = Irq;
  448. PCI_DN(node)->LogicalSlot = BridgeInfo->logicalSlotNumber;
  449. } /* for (Function = 0; Function < 8; ++Function) */
  450. } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
  451. return HvRc;
  452. }
  453. /*
  454. * I/0 Memory copy MUST use mmio commands on iSeries
  455. * To do; For performance, include the hv call directly
  456. */
  457. void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
  458. {
  459. u8 ByteValue = c;
  460. long NumberOfBytes = Count;
  461. while (NumberOfBytes > 0) {
  462. iSeries_Write_Byte(ByteValue, dest++);
  463. -- NumberOfBytes;
  464. }
  465. }
  466. EXPORT_SYMBOL(iSeries_memset_io);
  467. void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
  468. {
  469. char *src = source;
  470. long NumberOfBytes = count;
  471. while (NumberOfBytes > 0) {
  472. iSeries_Write_Byte(*src++, dest++);
  473. -- NumberOfBytes;
  474. }
  475. }
  476. EXPORT_SYMBOL(iSeries_memcpy_toio);
  477. void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
  478. {
  479. char *dst = dest;
  480. long NumberOfBytes = count;
  481. while (NumberOfBytes > 0) {
  482. *dst++ = iSeries_Read_Byte(src++);
  483. -- NumberOfBytes;
  484. }
  485. }
  486. EXPORT_SYMBOL(iSeries_memcpy_fromio);
  487. /*
  488. * Look down the chain to find the matching Device Device
  489. */
  490. static struct device_node *find_Device_Node(int bus, int devfn)
  491. {
  492. struct list_head *pos;
  493. list_for_each(pos, &iSeries_Global_Device_List) {
  494. struct device_node *node =
  495. list_entry(pos, struct device_node, Device_List);
  496. if ((bus == ISERIES_BUS(node)) &&
  497. (devfn == PCI_DN(node)->devfn))
  498. return node;
  499. }
  500. return NULL;
  501. }
  502. #if 0
  503. /*
  504. * Returns the device node for the passed pci_dev
  505. * Sanity Check Node PciDev to passed pci_dev
  506. * If none is found, returns a NULL which the client must handle.
  507. */
  508. static struct device_node *get_Device_Node(struct pci_dev *pdev)
  509. {
  510. struct device_node *node;
  511. node = pdev->sysdata;
  512. if (node == NULL || PCI_DN(node)->pcidev != pdev)
  513. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  514. return node;
  515. }
  516. #endif
  517. /*
  518. * Config space read and write functions.
  519. * For now at least, we look for the device node for the bus and devfn
  520. * that we are asked to access. It may be possible to translate the devfn
  521. * to a subbus and deviceid more directly.
  522. */
  523. static u64 hv_cfg_read_func[4] = {
  524. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  525. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  526. };
  527. static u64 hv_cfg_write_func[4] = {
  528. HvCallPciConfigStore8, HvCallPciConfigStore16,
  529. HvCallPciConfigStore32, HvCallPciConfigStore32
  530. };
  531. /*
  532. * Read PCI config space
  533. */
  534. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  535. int offset, int size, u32 *val)
  536. {
  537. struct device_node *node = find_Device_Node(bus->number, devfn);
  538. u64 fn;
  539. struct HvCallPci_LoadReturn ret;
  540. if (node == NULL)
  541. return PCIBIOS_DEVICE_NOT_FOUND;
  542. if (offset > 255) {
  543. *val = ~0;
  544. return PCIBIOS_BAD_REGISTER_NUMBER;
  545. }
  546. fn = hv_cfg_read_func[(size - 1) & 3];
  547. HvCall3Ret16(fn, &ret, PCI_DN(node)->DsaAddr.DsaAddr, offset, 0);
  548. if (ret.rc != 0) {
  549. *val = ~0;
  550. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  551. }
  552. *val = ret.value;
  553. return 0;
  554. }
  555. /*
  556. * Write PCI config space
  557. */
  558. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  559. int offset, int size, u32 val)
  560. {
  561. struct device_node *node = find_Device_Node(bus->number, devfn);
  562. u64 fn;
  563. u64 ret;
  564. if (node == NULL)
  565. return PCIBIOS_DEVICE_NOT_FOUND;
  566. if (offset > 255)
  567. return PCIBIOS_BAD_REGISTER_NUMBER;
  568. fn = hv_cfg_write_func[(size - 1) & 3];
  569. ret = HvCall4(fn, PCI_DN(node)->DsaAddr.DsaAddr, offset, val, 0);
  570. if (ret != 0)
  571. return PCIBIOS_DEVICE_NOT_FOUND;
  572. return 0;
  573. }
  574. static struct pci_ops iSeries_pci_ops = {
  575. .read = iSeries_pci_read_config,
  576. .write = iSeries_pci_write_config
  577. };
  578. /*
  579. * Check Return Code
  580. * -> On Failure, print and log information.
  581. * Increment Retry Count, if exceeds max, panic partition.
  582. *
  583. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  584. * PCI: Device 23.90 ReadL Retry( 1)
  585. * PCI: Device 23.90 ReadL Retry Successful(1)
  586. */
  587. static int CheckReturnCode(char *TextHdr, struct device_node *DevNode,
  588. int *retry, u64 ret)
  589. {
  590. if (ret != 0) {
  591. struct pci_dn *pdn = PCI_DN(DevNode);
  592. ++Pci_Error_Count;
  593. (*retry)++;
  594. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  595. TextHdr, pdn->DsaAddr.Dsa.busNumber, pdn->devfn,
  596. *retry, (int)ret);
  597. /*
  598. * Bump the retry and check for retry count exceeded.
  599. * If, Exceeded, panic the system.
  600. */
  601. if (((*retry) > Pci_Retry_Max) &&
  602. (Pci_Error_Flag > 0)) {
  603. mf_display_src(0xB6000103);
  604. panic_timeout = 0;
  605. panic("PCI: Hardware I/O Error, SRC B6000103, "
  606. "Automatic Reboot Disabled.\n");
  607. }
  608. return -1; /* Retry Try */
  609. }
  610. return 0;
  611. }
  612. /*
  613. * Translate the I/O Address into a device node, bar, and bar offset.
  614. * Note: Make sure the passed variable end up on the stack to avoid
  615. * the exposure of being device global.
  616. */
  617. static inline struct device_node *xlate_iomm_address(
  618. const volatile void __iomem *IoAddress,
  619. u64 *dsaptr, u64 *BarOffsetPtr)
  620. {
  621. unsigned long OrigIoAddr;
  622. unsigned long BaseIoAddr;
  623. unsigned long TableIndex;
  624. struct device_node *DevNode;
  625. OrigIoAddr = (unsigned long __force)IoAddress;
  626. if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
  627. return NULL;
  628. BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
  629. TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
  630. DevNode = iomm_table[TableIndex];
  631. if (DevNode != NULL) {
  632. int barnum = iobar_table[TableIndex];
  633. *dsaptr = PCI_DN(DevNode)->DsaAddr.DsaAddr | (barnum << 24);
  634. *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
  635. } else
  636. panic("PCI: Invalid PCI IoAddress detected!\n");
  637. return DevNode;
  638. }
  639. /*
  640. * Read MM I/O Instructions for the iSeries
  641. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  642. * else, data is returned in big Endian format.
  643. *
  644. * iSeries_Read_Byte = Read Byte ( 8 bit)
  645. * iSeries_Read_Word = Read Word (16 bit)
  646. * iSeries_Read_Long = Read Long (32 bit)
  647. */
  648. u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
  649. {
  650. u64 BarOffset;
  651. u64 dsa;
  652. int retry = 0;
  653. struct HvCallPci_LoadReturn ret;
  654. struct device_node *DevNode =
  655. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  656. if (DevNode == NULL) {
  657. static unsigned long last_jiffies;
  658. static int num_printed;
  659. if ((jiffies - last_jiffies) > 60 * HZ) {
  660. last_jiffies = jiffies;
  661. num_printed = 0;
  662. }
  663. if (num_printed++ < 10)
  664. printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
  665. return 0xff;
  666. }
  667. do {
  668. ++Pci_Io_Read_Count;
  669. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
  670. } while (CheckReturnCode("RDB", DevNode, &retry, ret.rc) != 0);
  671. return (u8)ret.value;
  672. }
  673. EXPORT_SYMBOL(iSeries_Read_Byte);
  674. u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
  675. {
  676. u64 BarOffset;
  677. u64 dsa;
  678. int retry = 0;
  679. struct HvCallPci_LoadReturn ret;
  680. struct device_node *DevNode =
  681. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  682. if (DevNode == NULL) {
  683. static unsigned long last_jiffies;
  684. static int num_printed;
  685. if ((jiffies - last_jiffies) > 60 * HZ) {
  686. last_jiffies = jiffies;
  687. num_printed = 0;
  688. }
  689. if (num_printed++ < 10)
  690. printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
  691. return 0xffff;
  692. }
  693. do {
  694. ++Pci_Io_Read_Count;
  695. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  696. BarOffset, 0);
  697. } while (CheckReturnCode("RDW", DevNode, &retry, ret.rc) != 0);
  698. return swab16((u16)ret.value);
  699. }
  700. EXPORT_SYMBOL(iSeries_Read_Word);
  701. u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
  702. {
  703. u64 BarOffset;
  704. u64 dsa;
  705. int retry = 0;
  706. struct HvCallPci_LoadReturn ret;
  707. struct device_node *DevNode =
  708. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  709. if (DevNode == NULL) {
  710. static unsigned long last_jiffies;
  711. static int num_printed;
  712. if ((jiffies - last_jiffies) > 60 * HZ) {
  713. last_jiffies = jiffies;
  714. num_printed = 0;
  715. }
  716. if (num_printed++ < 10)
  717. printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
  718. return 0xffffffff;
  719. }
  720. do {
  721. ++Pci_Io_Read_Count;
  722. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  723. BarOffset, 0);
  724. } while (CheckReturnCode("RDL", DevNode, &retry, ret.rc) != 0);
  725. return swab32((u32)ret.value);
  726. }
  727. EXPORT_SYMBOL(iSeries_Read_Long);
  728. /*
  729. * Write MM I/O Instructions for the iSeries
  730. *
  731. * iSeries_Write_Byte = Write Byte (8 bit)
  732. * iSeries_Write_Word = Write Word(16 bit)
  733. * iSeries_Write_Long = Write Long(32 bit)
  734. */
  735. void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
  736. {
  737. u64 BarOffset;
  738. u64 dsa;
  739. int retry = 0;
  740. u64 rc;
  741. struct device_node *DevNode =
  742. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  743. if (DevNode == NULL) {
  744. static unsigned long last_jiffies;
  745. static int num_printed;
  746. if ((jiffies - last_jiffies) > 60 * HZ) {
  747. last_jiffies = jiffies;
  748. num_printed = 0;
  749. }
  750. if (num_printed++ < 10)
  751. printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
  752. return;
  753. }
  754. do {
  755. ++Pci_Io_Write_Count;
  756. rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
  757. } while (CheckReturnCode("WWB", DevNode, &retry, rc) != 0);
  758. }
  759. EXPORT_SYMBOL(iSeries_Write_Byte);
  760. void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
  761. {
  762. u64 BarOffset;
  763. u64 dsa;
  764. int retry = 0;
  765. u64 rc;
  766. struct device_node *DevNode =
  767. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  768. if (DevNode == NULL) {
  769. static unsigned long last_jiffies;
  770. static int num_printed;
  771. if ((jiffies - last_jiffies) > 60 * HZ) {
  772. last_jiffies = jiffies;
  773. num_printed = 0;
  774. }
  775. if (num_printed++ < 10)
  776. printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
  777. return;
  778. }
  779. do {
  780. ++Pci_Io_Write_Count;
  781. rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
  782. } while (CheckReturnCode("WWW", DevNode, &retry, rc) != 0);
  783. }
  784. EXPORT_SYMBOL(iSeries_Write_Word);
  785. void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
  786. {
  787. u64 BarOffset;
  788. u64 dsa;
  789. int retry = 0;
  790. u64 rc;
  791. struct device_node *DevNode =
  792. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  793. if (DevNode == NULL) {
  794. static unsigned long last_jiffies;
  795. static int num_printed;
  796. if ((jiffies - last_jiffies) > 60 * HZ) {
  797. last_jiffies = jiffies;
  798. num_printed = 0;
  799. }
  800. if (num_printed++ < 10)
  801. printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
  802. return;
  803. }
  804. do {
  805. ++Pci_Io_Write_Count;
  806. rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
  807. } while (CheckReturnCode("WWL", DevNode, &retry, rc) != 0);
  808. }
  809. EXPORT_SYMBOL(iSeries_Write_Long);