2N/A/* pci.c - Generic PCI interfaces. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2007,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/pci.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm_private.h>
2N/A#include <grub/cache.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A/* FIXME: correctly support 64-bit architectures. */
2N/A/* #if GRUB_TARGET_SIZEOF_VOID_P == 4 */
2N/Astruct grub_pci_dma_chunk *
2N/Agrub_memalign_dma32 (grub_size_t align, grub_size_t size)
2N/A{
2N/A void *ret;
2N/A if (align < 64)
2N/A align = 64;
2N/A size = ALIGN_UP (size, align);
2N/A ret = grub_memalign (align, size);
2N/A if (!ret)
2N/A return 0;
2N/A grub_arch_sync_dma_caches (ret, size);
2N/A return ret;
2N/A}
2N/A
2N/A/* FIXME: evil. */
2N/Avoid
2N/Agrub_dma_free (struct grub_pci_dma_chunk *ch)
2N/A{
2N/A grub_size_t size = (((struct grub_mm_header *) ch) - 1)->size * GRUB_MM_ALIGN;
2N/A grub_arch_sync_dma_caches (ch, size);
2N/A grub_free (ch);
2N/A}
2N/A/* #endif */
2N/A
2N/A#ifdef GRUB_MACHINE_MIPS_LOONGSON
2N/Avolatile void *
2N/Agrub_dma_get_virt (struct grub_pci_dma_chunk *ch)
2N/A{
2N/A return (void *) ((((grub_uint32_t) ch) & 0x1fffffff) | 0xa0000000);
2N/A}
2N/A
2N/Agrub_uint32_t
2N/Agrub_dma_get_phys (struct grub_pci_dma_chunk *ch)
2N/A{
2N/A return (((grub_uint32_t) ch) & 0x1fffffff) | 0x80000000;
2N/A}
2N/A#else
2N/A
2N/Avolatile void *
2N/Agrub_dma_get_virt (struct grub_pci_dma_chunk *ch)
2N/A{
2N/A return (void *) ch;
2N/A}
2N/A
2N/Agrub_uint32_t
2N/Agrub_dma_get_phys (struct grub_pci_dma_chunk *ch)
2N/A{
2N/A return (grub_uint32_t) (grub_addr_t) ch;
2N/A}
2N/A
2N/A#endif
2N/A
2N/Agrub_pci_address_t
2N/Agrub_pci_make_address (grub_pci_device_t dev, int reg)
2N/A{
2N/A return (1 << 31) | (dev.bus << 16) | (dev.device << 11)
2N/A | (dev.function << 8) | reg;
2N/A}
2N/A
2N/Avoid
2N/Agrub_pci_iterate (grub_pci_iteratefunc_t hook)
2N/A{
2N/A grub_pci_device_t dev;
2N/A grub_pci_address_t addr;
2N/A grub_pci_id_t id;
2N/A grub_uint32_t hdr;
2N/A
2N/A for (dev.bus = 0; dev.bus < GRUB_PCI_NUM_BUS; dev.bus++)
2N/A {
2N/A for (dev.device = 0; dev.device < GRUB_PCI_NUM_DEVICES; dev.device++)
2N/A {
2N/A for (dev.function = 0; dev.function < 8; dev.function++)
2N/A {
2N/A addr = grub_pci_make_address (dev, GRUB_PCI_REG_PCI_ID);
2N/A id = grub_pci_read (addr);
2N/A
2N/A /* Check if there is a device present. */
2N/A if (id >> 16 == 0xFFFF)
2N/A {
2N/A if (dev.function == 0)
2N/A /* Devices are required to implement function 0, so if
2N/A it's missing then there is no device here. */
2N/A break;
2N/A else
2N/A continue;
2N/A }
2N/A
2N/A if (hook (dev, id))
2N/A return;
2N/A
2N/A /* Probe only func = 0 if the device if not multifunction */
2N/A if (dev.function == 0)
2N/A {
2N/A addr = grub_pci_make_address (dev, GRUB_PCI_REG_CACHELINE);
2N/A hdr = grub_pci_read (addr);
2N/A if (!(hdr & 0x800000))
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A}