14f5e7dd7Smrg/* 24f5e7dd7Smrg * (C) Copyright IBM Corporation 2007 34f5e7dd7Smrg * All Rights Reserved. 44f5e7dd7Smrg * 54f5e7dd7Smrg * Permission is hereby granted, free of charge, to any person obtaining a 64f5e7dd7Smrg * copy of this software and associated documentation files (the "Software"), 74f5e7dd7Smrg * to deal in the Software without restriction, including without limitation 84f5e7dd7Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub 94f5e7dd7Smrg * license, and/or sell copies of the Software, and to permit persons to whom 104f5e7dd7Smrg * the Software is furnished to do so, subject to the following conditions: 114f5e7dd7Smrg * 124f5e7dd7Smrg * The above copyright notice and this permission notice (including the next 134f5e7dd7Smrg * paragraph) shall be included in all copies or substantial portions of the 144f5e7dd7Smrg * Software. 154f5e7dd7Smrg * 164f5e7dd7Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 174f5e7dd7Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 184f5e7dd7Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 194f5e7dd7Smrg * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 204f5e7dd7Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 214f5e7dd7Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 224f5e7dd7Smrg * DEALINGS IN THE SOFTWARE. 234f5e7dd7Smrg */ 244f5e7dd7Smrg 254f5e7dd7Smrg/** 264f5e7dd7Smrg * \file linux_devmem.c 274f5e7dd7Smrg * Access PCI subsystem using Linux's the old /dev/mem interface. 28cad31331Smrg * 294f5e7dd7Smrg * \note 304f5e7dd7Smrg * This is currently just a skeleton. It only includes the /dev/mem based 314f5e7dd7Smrg * function for reading the device ROM. 324f5e7dd7Smrg * 334f5e7dd7Smrg * \author Ian Romanick <idr@us.ibm.com> 344f5e7dd7Smrg */ 3549310723Smrg#ifdef HAVE_CONFIG_H 3649310723Smrg#include "config.h" 3749310723Smrg#endif 384f5e7dd7Smrg 394f5e7dd7Smrg#include <stdlib.h> 404f5e7dd7Smrg#include <string.h> 414f5e7dd7Smrg#include <stdio.h> 424f5e7dd7Smrg#include <unistd.h> 434f5e7dd7Smrg#include <sys/types.h> 444f5e7dd7Smrg#include <sys/stat.h> 454f5e7dd7Smrg#include <fcntl.h> 464f5e7dd7Smrg#include <sys/mman.h> 474f5e7dd7Smrg#include <dirent.h> 484f5e7dd7Smrg#include <errno.h> 494f5e7dd7Smrg 504f5e7dd7Smrg#include "pciaccess.h" 514f5e7dd7Smrg#include "pciaccess_private.h" 524f5e7dd7Smrg#include "linux_devmem.h" 534f5e7dd7Smrg 544f5e7dd7Smrg/** 554f5e7dd7Smrg * Read a device's expansion ROM using /dev/mem. 56cad31331Smrg * 574f5e7dd7Smrg * \note 584f5e7dd7Smrg * This function could probably be used, as-is, on other platforms that have 594f5e7dd7Smrg * a /dev/mem interface. 60cad31331Smrg * 614f5e7dd7Smrg * \bugs 624f5e7dd7Smrg * Before using the VGA special case code, this function should check that 634f5e7dd7Smrg * VGA access are routed to the device. Right? 644f5e7dd7Smrg */ 654f5e7dd7Smrg_pci_hidden int 664f5e7dd7Smrgpci_device_linux_devmem_read_rom(struct pci_device *dev, void *buffer) 674f5e7dd7Smrg{ 684f5e7dd7Smrg struct pci_device_private *priv = (struct pci_device_private *) dev; 694f5e7dd7Smrg int fd; 704f5e7dd7Smrg int err = 0; 714f5e7dd7Smrg uint32_t rom_base_tmp; 724f5e7dd7Smrg pciaddr_t rom_base; 734f5e7dd7Smrg pciaddr_t rom_size; 744f5e7dd7Smrg int PCI_ROM; 754f5e7dd7Smrg 764f5e7dd7Smrg 774f5e7dd7Smrg /* Handle some special cases of legacy devices. 784f5e7dd7Smrg */ 794f5e7dd7Smrg if (priv->base.rom_size == 0) { 804f5e7dd7Smrg /* VGA ROMs are supposed to be at 0xC0000. 814f5e7dd7Smrg */ 824f5e7dd7Smrg if ((priv->base.device_class & 0x00ffff00) == 0x000030000) { 834f5e7dd7Smrg rom_base = 0x000C0000; 844f5e7dd7Smrg rom_size = 0x00010000; 854f5e7dd7Smrg PCI_ROM = 0; 864f5e7dd7Smrg } 874f5e7dd7Smrg else { 884f5e7dd7Smrg /* "Function not implemented." 894f5e7dd7Smrg */ 904f5e7dd7Smrg return ENOSYS; 914f5e7dd7Smrg } 924f5e7dd7Smrg } 934f5e7dd7Smrg else { 944f5e7dd7Smrg rom_base = priv->rom_base; 954f5e7dd7Smrg rom_size = priv->base.rom_size; 964f5e7dd7Smrg PCI_ROM = 1; 974f5e7dd7Smrg } 98cad31331Smrg 994f5e7dd7Smrg 1004f5e7dd7Smrg 1014f5e7dd7Smrg /* Enable the device's ROM. 1024f5e7dd7Smrg */ 1034f5e7dd7Smrg if (PCI_ROM) { 1044f5e7dd7Smrg err = pci_device_cfg_read_u32(& priv->base, & rom_base_tmp, 48); 1054f5e7dd7Smrg if (err) { 1064f5e7dd7Smrg return err; 1074f5e7dd7Smrg } 1084f5e7dd7Smrg 1094f5e7dd7Smrg if ((rom_base_tmp & 0x000000001) == 0) { 110cad31331Smrg err = pci_device_cfg_write_u32(& priv->base, 1114f5e7dd7Smrg rom_base_tmp | 1, 48); 1124f5e7dd7Smrg if (err) { 1134f5e7dd7Smrg return err; 1144f5e7dd7Smrg } 1154f5e7dd7Smrg } 1164f5e7dd7Smrg } 117cad31331Smrg 118cad31331Smrg 1194f5e7dd7Smrg /* Read the portion of /dev/mem that corresponds to the device's ROM. 1204f5e7dd7Smrg */ 1214f5e7dd7Smrg fd = open("/dev/mem", O_RDONLY, 0); 1224f5e7dd7Smrg if (fd < 0) { 1234f5e7dd7Smrg err = errno; 1244f5e7dd7Smrg } 1254f5e7dd7Smrg else { 1264f5e7dd7Smrg size_t bytes; 1274f5e7dd7Smrg 1284f5e7dd7Smrg for (bytes = 0; bytes < rom_size; /* empty */) { 129cad31331Smrg const ssize_t got = pread(fd, buffer, rom_size - bytes, 1304f5e7dd7Smrg rom_base + bytes); 1314f5e7dd7Smrg if (got == -1) { 1324f5e7dd7Smrg err = errno; 1334f5e7dd7Smrg break; 1344f5e7dd7Smrg } 1354f5e7dd7Smrg 1364f5e7dd7Smrg bytes += got; 1374f5e7dd7Smrg } 1384f5e7dd7Smrg 1394f5e7dd7Smrg close(fd); 1404f5e7dd7Smrg } 1414f5e7dd7Smrg 142cad31331Smrg 1434f5e7dd7Smrg /* Disable the device's ROM. 1444f5e7dd7Smrg */ 1454f5e7dd7Smrg if (PCI_ROM && ((rom_base_tmp & 0x000000001) == 0)) { 1464f5e7dd7Smrg const int tmp_err = pci_device_cfg_write_u32(& priv->base, 1474f5e7dd7Smrg rom_base_tmp, 48); 1484f5e7dd7Smrg 1494f5e7dd7Smrg /* Prefer to return the first error that occurred. 1504f5e7dd7Smrg */ 1514f5e7dd7Smrg if (err == 0) { 1524f5e7dd7Smrg err = tmp_err; 1534f5e7dd7Smrg } 1544f5e7dd7Smrg } 1554f5e7dd7Smrg 1564f5e7dd7Smrg return err; 1574f5e7dd7Smrg} 158