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/abs_addr.h>
  37. #include <asm/iSeries/HvCallPci.h>
  38. #include <asm/iSeries/HvCallXm.h>
  39. #include <asm/iSeries/mf.h>
  40. #include <asm/ppc-pci.h>
  41. #include "irq.h"
  42. #include "pci.h"
  43. extern unsigned long io_page_mask;
  44. /*
  45. * Forward declares of prototypes.
  46. */
  47. static struct device_node *find_Device_Node(int bus, int devfn);
  48. static void scan_PHB_slots(struct pci_controller *Phb);
  49. static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
  50. static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
  51. LIST_HEAD(iSeries_Global_Device_List);
  52. static int DeviceCount;
  53. /* Counters and control flags. */
  54. static long Pci_Io_Read_Count;
  55. static long Pci_Io_Write_Count;
  56. #if 0
  57. static long Pci_Cfg_Read_Count;
  58. static long Pci_Cfg_Write_Count;
  59. #endif
  60. static long Pci_Error_Count;
  61. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  62. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  63. static struct pci_ops iSeries_pci_ops;
  64. /*
  65. * Table defines
  66. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  67. */
  68. #define IOMM_TABLE_MAX_ENTRIES 1024
  69. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  70. #define BASE_IO_MEMORY 0xE000000000000000UL
  71. static unsigned long max_io_memory = 0xE000000000000000UL;
  72. static long current_iomm_table_entry;
  73. /*
  74. * Lookup Tables.
  75. */
  76. static struct device_node **iomm_table;
  77. static u8 *iobar_table;
  78. /*
  79. * Static and Global variables
  80. */
  81. static char *pci_io_text = "iSeries PCI I/O";
  82. static DEFINE_SPINLOCK(iomm_table_lock);
  83. /*
  84. * iomm_table_initialize
  85. *
  86. * Allocates and initalizes the Address Translation Table and Bar
  87. * Tables to get them ready for use. Must be called before any
  88. * I/O space is handed out to the device BARs.
  89. */
  90. static void iomm_table_initialize(void)
  91. {
  92. spin_lock(&iomm_table_lock);
  93. iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
  94. GFP_KERNEL);
  95. iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
  96. GFP_KERNEL);
  97. spin_unlock(&iomm_table_lock);
  98. if ((iomm_table == NULL) || (iobar_table == NULL))
  99. panic("PCI: I/O tables allocation failed.\n");
  100. }
  101. /*
  102. * iomm_table_allocate_entry
  103. *
  104. * Adds pci_dev entry in address translation table
  105. *
  106. * - Allocates the number of entries required in table base on BAR
  107. * size.
  108. * - Allocates starting at BASE_IO_MEMORY and increases.
  109. * - The size is round up to be a multiple of entry size.
  110. * - CurrentIndex is incremented to keep track of the last entry.
  111. * - Builds the resource entry for allocated BARs.
  112. */
  113. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  114. {
  115. struct resource *bar_res = &dev->resource[bar_num];
  116. long bar_size = pci_resource_len(dev, bar_num);
  117. /*
  118. * No space to allocate, quick exit, skip Allocation.
  119. */
  120. if (bar_size == 0)
  121. return;
  122. /*
  123. * Set Resource values.
  124. */
  125. spin_lock(&iomm_table_lock);
  126. bar_res->name = pci_io_text;
  127. bar_res->start =
  128. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  129. bar_res->start += BASE_IO_MEMORY;
  130. bar_res->end = bar_res->start + bar_size - 1;
  131. /*
  132. * Allocate the number of table entries needed for BAR.
  133. */
  134. while (bar_size > 0 ) {
  135. iomm_table[current_iomm_table_entry] = dev->sysdata;
  136. iobar_table[current_iomm_table_entry] = bar_num;
  137. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  138. ++current_iomm_table_entry;
  139. }
  140. max_io_memory = BASE_IO_MEMORY +
  141. (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
  142. spin_unlock(&iomm_table_lock);
  143. }
  144. /*
  145. * allocate_device_bars
  146. *
  147. * - Allocates ALL pci_dev BAR's and updates the resources with the
  148. * BAR value. BARS with zero length will have the resources
  149. * The HvCallPci_getBarParms is used to get the size of the BAR
  150. * space. It calls iomm_table_allocate_entry to allocate
  151. * each entry.
  152. * - Loops through The Bar resources(0 - 5) including the ROM
  153. * is resource(6).
  154. */
  155. static void allocate_device_bars(struct pci_dev *dev)
  156. {
  157. struct resource *bar_res;
  158. int bar_num;
  159. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
  160. bar_res = &dev->resource[bar_num];
  161. iomm_table_allocate_entry(dev, bar_num);
  162. }
  163. }
  164. /*
  165. * Log error information to system console.
  166. * Filter out the device not there errors.
  167. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  168. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  169. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  170. */
  171. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  172. int AgentId, int HvRc)
  173. {
  174. if (HvRc == 0x0302)
  175. return;
  176. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  177. Error_Text, Bus, SubBus, AgentId, HvRc);
  178. }
  179. /*
  180. * build_device_node(u16 Bus, int SubBus, u8 DevFn)
  181. */
  182. static struct device_node *build_device_node(HvBusNumber Bus,
  183. HvSubBusNumber SubBus, int AgentId, int Function)
  184. {
  185. struct device_node *node;
  186. struct pci_dn *pdn;
  187. PPCDBG(PPCDBG_BUSWALK,
  188. "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
  189. Bus, SubBus, AgentId, Function);
  190. node = kmalloc(sizeof(struct device_node), GFP_KERNEL);
  191. if (node == NULL)
  192. return NULL;
  193. memset(node, 0, sizeof(struct device_node));
  194. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  195. if (pdn == NULL) {
  196. kfree(node);
  197. return NULL;
  198. }
  199. node->data = pdn;
  200. pdn->node = node;
  201. list_add_tail(&pdn->Device_List, &iSeries_Global_Device_List);
  202. #if 0
  203. pdn->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
  204. #endif
  205. pdn->DsaAddr.DsaAddr = 0;
  206. pdn->DsaAddr.Dsa.busNumber = Bus;
  207. pdn->DsaAddr.Dsa.subBusNumber = SubBus;
  208. pdn->DsaAddr.Dsa.deviceId = 0x10;
  209. pdn->devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
  210. return node;
  211. }
  212. /*
  213. * unsigned long __init find_and_init_phbs(void)
  214. *
  215. * Description:
  216. * This function checks for all possible system PCI host bridges that connect
  217. * PCI buses. The system hypervisor is queried as to the guest partition
  218. * ownership status. A pci_controller is built for any bus which is partially
  219. * owned or fully owned by this guest partition.
  220. */
  221. unsigned long __init find_and_init_phbs(void)
  222. {
  223. struct pci_controller *phb;
  224. HvBusNumber bus;
  225. PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
  226. /* Check all possible buses. */
  227. for (bus = 0; bus < 256; bus++) {
  228. int ret = HvCallXm_testBus(bus);
  229. if (ret == 0) {
  230. printk("bus %d appears to exist\n", bus);
  231. phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  232. if (phb == NULL)
  233. return -ENOMEM;
  234. pci_setup_pci_controller(phb);
  235. phb->pci_mem_offset = phb->local_number = bus;
  236. phb->first_busno = bus;
  237. phb->last_busno = bus;
  238. phb->ops = &iSeries_pci_ops;
  239. PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
  240. phb, bus);
  241. /* Find and connect the devices. */
  242. scan_PHB_slots(phb);
  243. }
  244. /*
  245. * Check for Unexpected Return code, a clue that something
  246. * has gone wrong.
  247. */
  248. else if (ret != 0x0301)
  249. printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
  250. bus, ret);
  251. }
  252. return 0;
  253. }
  254. /*
  255. * iSeries_pcibios_init
  256. *
  257. * Chance to initialize and structures or variable before PCI Bus walk.
  258. */
  259. void iSeries_pcibios_init(void)
  260. {
  261. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n");
  262. iomm_table_initialize();
  263. find_and_init_phbs();
  264. io_page_mask = -1;
  265. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n");
  266. }
  267. /*
  268. * iSeries_pci_final_fixup(void)
  269. */
  270. void __init iSeries_pci_final_fixup(void)
  271. {
  272. struct pci_dev *pdev = NULL;
  273. struct device_node *node;
  274. int DeviceCount = 0;
  275. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n");
  276. /* Fix up at the device node and pci_dev relationship */
  277. mf_display_src(0xC9000100);
  278. printk("pcibios_final_fixup\n");
  279. for_each_pci_dev(pdev) {
  280. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  281. printk("pci dev %p (%x.%x), node %p\n", pdev,
  282. pdev->bus->number, pdev->devfn, node);
  283. if (node != NULL) {
  284. ++DeviceCount;
  285. pdev->sysdata = (void *)node;
  286. PCI_DN(node)->pcidev = pdev;
  287. PPCDBG(PPCDBG_BUSWALK,
  288. "pdev 0x%p <==> DevNode 0x%p\n",
  289. pdev, node);
  290. allocate_device_bars(pdev);
  291. iSeries_Device_Information(pdev, DeviceCount);
  292. iommu_devnode_init_iSeries(node);
  293. } else
  294. printk("PCI: Device Tree not found for 0x%016lX\n",
  295. (unsigned long)pdev);
  296. pdev->irq = PCI_DN(node)->Irq;
  297. }
  298. iSeries_activate_IRQs();
  299. mf_display_src(0xC9000200);
  300. }
  301. void pcibios_fixup_bus(struct pci_bus *PciBus)
  302. {
  303. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
  304. PciBus->number);
  305. }
  306. void pcibios_fixup_resources(struct pci_dev *pdev)
  307. {
  308. PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
  309. }
  310. /*
  311. * Loop through each node function to find usable EADs bridges.
  312. */
  313. static void scan_PHB_slots(struct pci_controller *Phb)
  314. {
  315. struct HvCallPci_DeviceInfo *DevInfo;
  316. HvBusNumber bus = Phb->local_number; /* System Bus */
  317. const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
  318. int HvRc = 0;
  319. int IdSel;
  320. const int MaxAgents = 8;
  321. DevInfo = (struct HvCallPci_DeviceInfo*)
  322. kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
  323. if (DevInfo == NULL)
  324. return;
  325. /*
  326. * Probe for EADs Bridges
  327. */
  328. for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
  329. HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
  330. iseries_hv_addr(DevInfo),
  331. sizeof(struct HvCallPci_DeviceInfo));
  332. if (HvRc == 0) {
  333. if (DevInfo->deviceType == HvCallPci_NodeDevice)
  334. scan_EADS_bridge(bus, SubBus, IdSel);
  335. else
  336. printk("PCI: Invalid System Configuration(0x%02X)"
  337. " for bus 0x%02x id 0x%02x.\n",
  338. DevInfo->deviceType, bus, IdSel);
  339. }
  340. else
  341. pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
  342. }
  343. kfree(DevInfo);
  344. }
  345. static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
  346. int IdSel)
  347. {
  348. struct HvCallPci_BridgeInfo *BridgeInfo;
  349. HvAgentId AgentId;
  350. int Function;
  351. int HvRc;
  352. BridgeInfo = (struct HvCallPci_BridgeInfo *)
  353. kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
  354. if (BridgeInfo == NULL)
  355. return;
  356. /* Note: hvSubBus and irq is always be 0 at this level! */
  357. for (Function = 0; Function < 8; ++Function) {
  358. AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  359. HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
  360. if (HvRc == 0) {
  361. printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
  362. bus, IdSel, Function, AgentId);
  363. /* Connect EADs: 0x18.00.12 = 0x00 */
  364. PPCDBG(PPCDBG_BUSWALK,
  365. "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
  366. bus, SubBus, AgentId);
  367. HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
  368. iseries_hv_addr(BridgeInfo),
  369. sizeof(struct HvCallPci_BridgeInfo));
  370. if (HvRc == 0) {
  371. printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
  372. BridgeInfo->busUnitInfo.deviceType,
  373. BridgeInfo->subBusNumber,
  374. BridgeInfo->maxAgents,
  375. BridgeInfo->maxSubBusNumber,
  376. BridgeInfo->logicalSlotNumber);
  377. PPCDBG(PPCDBG_BUSWALK,
  378. "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
  379. BridgeInfo->busUnitInfo.deviceType,
  380. BridgeInfo->subBusNumber,
  381. BridgeInfo->maxAgents,
  382. BridgeInfo->maxSubBusNumber,
  383. BridgeInfo->logicalSlotNumber);
  384. if (BridgeInfo->busUnitInfo.deviceType ==
  385. HvCallPci_BridgeDevice) {
  386. /* Scan_Bridge_Slot...: 0x18.00.12 */
  387. scan_bridge_slot(bus, BridgeInfo);
  388. } else
  389. printk("PCI: Invalid Bridge Configuration(0x%02X)",
  390. BridgeInfo->busUnitInfo.deviceType);
  391. }
  392. } else if (HvRc != 0x000B)
  393. pci_Log_Error("EADs Connect",
  394. bus, SubBus, AgentId, HvRc);
  395. }
  396. kfree(BridgeInfo);
  397. }
  398. /*
  399. * This assumes that the node slot is always on the primary bus!
  400. */
  401. static int scan_bridge_slot(HvBusNumber Bus,
  402. struct HvCallPci_BridgeInfo *BridgeInfo)
  403. {
  404. struct device_node *node;
  405. HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
  406. u16 VendorId = 0;
  407. int HvRc = 0;
  408. u8 Irq = 0;
  409. int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
  410. int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
  411. HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
  412. /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
  413. Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
  414. PPCDBG(PPCDBG_BUSWALK,
  415. "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
  416. Bus, 0, EADsIdSel, Irq);
  417. /*
  418. * Connect all functions of any device found.
  419. */
  420. for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
  421. for (Function = 0; Function < 8; ++Function) {
  422. HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  423. HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
  424. AgentId, Irq);
  425. if (HvRc != 0) {
  426. pci_Log_Error("Connect Bus Unit",
  427. Bus, SubBus, AgentId, HvRc);
  428. continue;
  429. }
  430. HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
  431. PCI_VENDOR_ID, &VendorId);
  432. if (HvRc != 0) {
  433. pci_Log_Error("Read Vendor",
  434. Bus, SubBus, AgentId, HvRc);
  435. continue;
  436. }
  437. printk("read vendor ID: %x\n", VendorId);
  438. /* FoundDevice: 0x18.28.10 = 0x12AE */
  439. PPCDBG(PPCDBG_BUSWALK,
  440. "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
  441. Bus, SubBus, AgentId, VendorId, Irq);
  442. HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
  443. PCI_INTERRUPT_LINE, Irq);
  444. if (HvRc != 0)
  445. pci_Log_Error("PciCfgStore Irq Failed!",
  446. Bus, SubBus, AgentId, HvRc);
  447. ++DeviceCount;
  448. node = build_device_node(Bus, SubBus, EADsIdSel, Function);
  449. PCI_DN(node)->Irq = Irq;
  450. PCI_DN(node)->LogicalSlot = BridgeInfo->logicalSlotNumber;
  451. } /* for (Function = 0; Function < 8; ++Function) */
  452. } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
  453. return HvRc;
  454. }
  455. /*
  456. * I/0 Memory copy MUST use mmio commands on iSeries
  457. * To do; For performance, include the hv call directly
  458. */
  459. void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
  460. {
  461. u8 ByteValue = c;
  462. long NumberOfBytes = Count;
  463. while (NumberOfBytes > 0) {
  464. iSeries_Write_Byte(ByteValue, dest++);
  465. -- NumberOfBytes;
  466. }
  467. }
  468. EXPORT_SYMBOL(iSeries_memset_io);
  469. void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
  470. {
  471. char *src = source;
  472. long NumberOfBytes = count;
  473. while (NumberOfBytes > 0) {
  474. iSeries_Write_Byte(*src++, dest++);
  475. -- NumberOfBytes;
  476. }
  477. }
  478. EXPORT_SYMBOL(iSeries_memcpy_toio);
  479. void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
  480. {
  481. char *dst = dest;
  482. long NumberOfBytes = count;
  483. while (NumberOfBytes > 0) {
  484. *dst++ = iSeries_Read_Byte(src++);
  485. -- NumberOfBytes;
  486. }
  487. }
  488. EXPORT_SYMBOL(iSeries_memcpy_fromio);
  489. /*
  490. * Look down the chain to find the matching Device Device
  491. */
  492. static struct device_node *find_Device_Node(int bus, int devfn)
  493. {
  494. struct pci_dn *pdn;
  495. list_for_each_entry(pdn, &iSeries_Global_Device_List, Device_List) {
  496. if ((bus == pdn->DsaAddr.Dsa.busNumber) &&
  497. (devfn == pdn->devfn))
  498. return pdn->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);