<commit_before>/**
 * \file
 * \brief PCI configuration space access
 */

/*
 * Copyright (c) 2008, 2009, ETH Zurich.
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
 */

#include <barrelfish/barrelfish.h>
#include <stdio.h>
#include <stdlib.h>
#include <mm/mm.h>
#include <acpi_client/acpi_client.h>
#include <pci/confspace/pci_confspace.h>

static uint8_t startbus, endbus;
static struct memobj_one_frame_lazy *memobj = NULL;
static struct vregion *vregion = NULL;
static struct capref region_cap;

/// Enable PCIe. Default is yes.
static bool pcie_enabled = true;

/* FIXME: XXX: super-hacky bitfield to track if we already mapped something */
static uint8_t *mapped_bitfield;

int pcie_confspace_init(struct mm* mem_allocator, lpaddr_t base, uint16_t segment, uint8_t startbusarg,
                        uint8_t endbusarg)
{
    int r;
    errval_t err;

    assert(segment == 0); // unhandled!

    startbus = startbusarg;
    endbus = endbusarg;

    // compute size of region
    size_t region_pages = (endbus + 1 - startbus) << 8;
    size_t region_bytes = region_pages * BASE_PAGE_SIZE;
    uint8_t region_bits = log2ceil(region_bytes);

    /* check that we have a cap for the whole region, and mark it allocated */
    struct capref ram_cap;
    if (mem_allocator) {
		r = mm_alloc_range(mem_allocator, region_bits, base,
						   base + (1UL << region_bits), &ram_cap, NULL);
    }
    else {
    	struct acpi_rpc_client* acl = get_acpi_rpc_client();
    	errval_t error_code;
    	r = acl->vtbl.mm_alloc_range_proxy(acl, region_bits, base,
    			base + (1UL << region_bits), &ram_cap, &error_code);
    	assert(r == 0);
    	r = error_code; // XXX: why has this code weird conventions like int r for errors?
    }
    if (r != 0) {
        //PCI_DEBUG("pci_confspace_init: allocating %lx-%lx failed\n",
        //       base, base + (1UL << region_bits));
        return r;
    }

    /* create frame cap to whole region */
    /*
    r = devframe_type(&region_cap, ram_cap, region_bits);
    assert(r == 0);
    if (r != 0) {
        return r;
    }*/


    memobj = malloc(sizeof(struct memobj_one_frame_lazy));
    assert(memobj);
    vregion = malloc(sizeof(struct vregion));
    assert(vregion);
    err = memobj_create_one_frame_lazy(memobj, region_bytes, 0, region_cap,
                                       BASE_PAGE_SIZE);
    if (err_is_fail(err)) {
        DEBUG_ERR(err, "memobj_create_one_frame_lazy failed");<commit_msg>Correct cap in confspace.<commit_after>/**
 * \file
 * \brief PCI configuration space access
 */

/*
 * Copyright (c) 2008, 2009, ETH Zurich.
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
 */

#include <barrelfish/barrelfish.h>
#include <stdio.h>
#include <stdlib.h>
#include <mm/mm.h>
#include <acpi_client/acpi_client.h>
#include <pci/confspace/pci_confspace.h>

static uint8_t startbus, endbus;
static struct memobj_one_frame_lazy *memobj = NULL;
static struct vregion *vregion = NULL;
static struct capref region_cap;

/// Enable PCIe. Default is yes.
static bool pcie_enabled = true;

/* FIXME: XXX: super-hacky bitfield to track if we already mapped something */
static uint8_t *mapped_bitfield;

int pcie_confspace_init(struct mm* mem_allocator, lpaddr_t base, uint16_t segment, uint8_t startbusarg,
                        uint8_t endbusarg)
{
    int r;
    errval_t err;

    assert(segment == 0); // unhandled!

    startbus = startbusarg;
    endbus = endbusarg;

    // compute size of region
    size_t region_pages = (endbus + 1 - startbus) << 8;
    size_t region_bytes = region_pages * BASE_PAGE_SIZE;
    uint8_t region_bits = log2ceil(region_bytes);

    /* check that we have a cap for the whole region, and mark it allocated */
    struct capref ram_cap;
    if (mem_allocator) {
		r = mm_alloc_range(mem_allocator, region_bits, base,
						   base + (1UL << region_bits), &ram_cap, NULL);
    }
    else {
    	struct acpi_rpc_client* acl = get_acpi_rpc_client();
    	errval_t error_code;
    	r = acl->vtbl.mm_alloc_range_proxy(acl, region_bits, base,
    			base + (1UL << region_bits), &ram_cap, &error_code);
    	assert(r == 0);
    	r = error_code; // XXX: why has this code weird conventions like int r for errors?
    }
    if (r != 0) {
        //PCI_DEBUG("pci_confspace_init: allocating %lx-%lx failed\n",
        //       base, base + (1UL << region_bits));
        return r;
    }

    /* create frame cap to whole region */
    /*
    r = devframe_type(&region_cap, ram_cap, region_bits);
    assert(r == 0);
    if (r != 0) {
        return r;
    }*/
    region_cap = ram_cap;


    memobj = malloc(sizeof(struct memobj_one_frame_lazy));
    assert(memobj);
    vregion = malloc(sizeof(struct vregion));
    assert(vregion);
    err = memobj_create_one_frame_lazy(memobj, region_bytes, 0, region_cap,
                                       BASE_PAGE_SIZE);
    if (err_is_fail(err)) {
        DEBUG_ERR(err, "memobj_create_one_frame_lazy failed");
{'fraction_non_alphanumeric': 0.10779902512185977, 'fraction_numerical': 0.011998500187476566, 'mean_word_length': 2.999250374812594, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1667455', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1810, 'n_words': 512}