aboutsummaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-06-24 22:55:29 -0400
committerIan Moffett <ian@osmora.org>2024-06-24 22:55:29 -0400
commit236963e7563be3e3f8220dac7bb4af446928e194 (patch)
treee521ea226db0345bbb3679fffe09d96254b7dc73 /sys/dev
parent214eadc62b5578f76c98a38a28f8b3d80ac4d6ad (diff)
Clean out for expt
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/ic/ahci.c771
-rw-r--r--sys/dev/ic/nvme.c614
-rw-r--r--sys/dev/pci/pci.c344
-rw-r--r--sys/dev/usb/xhci.c463
-rw-r--r--sys/dev/vcons/vcons.c227
-rw-r--r--sys/dev/vcons/vcons_io.c84
-rw-r--r--sys/dev/video/fbdev.c120
7 files changed, 0 insertions, 2623 deletions
diff --git a/sys/dev/ic/ahci.c b/sys/dev/ic/ahci.c
deleted file mode 100644
index 257fd00..0000000
--- a/sys/dev/ic/ahci.c
+++ /dev/null
@@ -1,771 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/driver.h>
-#include <sys/timer.h>
-#include <sys/types.h>
-#include <sys/cdefs.h>
-#include <sys/syslog.h>
-#include <sys/mmio.h>
-#include <sys/errno.h>
-#include <sys/device.h>
-#include <fs/devfs.h>
-#include <dev/pci/pci.h>
-#include <dev/ic/ahciregs.h>
-#include <dev/ic/ahcivar.h>
-#include <machine/bus.h>
-#include <vm/vm.h>
-#include <string.h>
-
-__KERNEL_META("$Hyra$: ahci.c, Ian Marco Moffett, "
- "AHCI driver");
-
-#define pr_trace(fmt, ...) kprintf("ahci: " fmt, ##__VA_ARGS__)
-#define pr_error(...) pr_trace(__VA_ARGS__)
-
-static TAILQ_HEAD(, ahci_device) sata_devs;
-
-static struct pci_device *dev;
-static struct timer driver_tmr;
-static struct mutex io_lock;
-static size_t dev_count = 0;
-
-static bool
-is_word_aligned(void *ptr)
-{
- return (((uintptr_t)ptr) & 1) == 0;
-}
-
-/*
- * Fetch a SATA device with a SATA device
- * minor.
- *
- * @dev_minor: SATA device minor.
- */
-static struct ahci_device *
-ahci_get_sata(dev_t dev_minor)
-{
- struct ahci_device *dev;
-
- TAILQ_FOREACH(dev, &sata_devs, link) {
- if (dev->minor == dev_minor) {
- return dev;
- }
- }
-
- return NULL;
-}
-
-/*
- * Poll register to have `bits' set/unset.
- *
- * @reg: Register to poll.
- * @bits: Bits expected to be set/unset.
- * @pollset: True to poll as set.
- */
-static int
-ahci_poll_reg32(volatile uint32_t *reg, uint32_t bits, bool pollset)
-{
- uint32_t time_waited = 0;
- uint32_t val;
- bool tmp;
-
- for (;;) {
- val = mmio_read32(reg);
- tmp = (pollset) ? __TEST(val, bits) : !__TEST(val, bits);
- if (tmp)
- break;
- if (time_waited >= AHCI_TIMEOUT)
- /* Timeout */
- return -1;
-
- driver_tmr.msleep(10);
- time_waited += 10;
- }
-
- return 0;
-}
-
-/*
- * Put the HBA in AHCI mode.
- */
-static inline void
-ahci_set_ahci(struct ahci_hba *hba)
-{
- struct hba_memspace *abar = hba->abar;
- uint32_t ghc;
-
- /* Enable AHCI mode */
- ghc = mmio_read32(&abar->ghc);
- ghc |= AHCI_GHC_AE;
- mmio_write32(&abar->ghc, ghc);
-}
-
-/*
- * Reset the HBA with GHC.HR
- *
- * XXX: The spec states that all port registers *except*
- * PxFB, PxFBU, PxCLB, PxCLBU are reset.
- */
-static int
-ahci_hba_reset(struct ahci_hba *hba)
-{
- struct hba_memspace *abar = hba->abar;
- uint32_t ghc;
- uint8_t attempts = 0;
- int status;
-
- ghc = mmio_read32(&abar->ghc);
- ghc |= AHCI_GHC_HR;
- mmio_write32(&abar->ghc, ghc);
-
- /*
- * Poll the GHC.HR bit. The HBA is supposed to flip
- * it back to zero once the reset is complete. If
- * the HBA does not do this, something is screwed
- * up.
- *
- * XXX: We do this twice in case of slow hardware...
- */
- while ((attempts++) < 2) {
- status = ahci_poll_reg32(&abar->ghc, AHCI_GHC_HR, false);
- if (status == 0) {
- break;
- }
- }
-
- /* We hope this doesn't happen */
- if (status != 0) {
- pr_error("HBA reset failure: GHC.HR stuck (HBA hung)\n");
- return status;
- }
-
- ahci_set_ahci(hba);
- return 0;
-}
-
-/*
- * Stop port and put it in an idle state.
- */
-static int
-ahci_stop_port(struct hba_port *port)
-{
- const uint32_t RUN_MASK = (AHCI_PXCMD_FR | AHCI_PXCMD_CR);
- uint32_t cmd = mmio_read32(&port->cmd);
-
- /* Check if it is already stopped */
- if (!__TEST(cmd, RUN_MASK))
- return 0;
-
- /*
- * Stop the FIS receive and disable proessing
- * of the command list.
- */
- cmd &= ~(AHCI_PXCMD_ST | AHCI_PXCMD_FRE);
- mmio_write32(&port->cmd, cmd);
- return ahci_poll_reg32(&port->cmd, RUN_MASK, false);
-}
-
-/*
- * Put a port in a running state.
- */
-static int
-ahci_start_port(struct hba_port *port)
-{
- const uint32_t RUN_MASK = (AHCI_PXCMD_FR | AHCI_PXCMD_CR);
- uint32_t cmd = mmio_read32(&port->cmd);
-
- /* Check if it is already running */
- if (__TEST(cmd, RUN_MASK))
- return 0;
-
- /* Start everything up */
- cmd |= (AHCI_PXCMD_ST | AHCI_PXCMD_FRE);
- mmio_write32(&port->cmd, cmd);
- return ahci_poll_reg32(&port->cmd, RUN_MASK, true);
-}
-
-/*
- * Check if a port is active.
- *
- * @port: Port to check.
- */
-static bool
-ahci_port_active(struct hba_port *port)
-{
- uint32_t ssts;
- uint8_t det, ipm;
-
- ssts = mmio_read32(&port->ssts);
- det = AHCI_PXSSTS_DET(ssts);
- ipm = AHCI_PXSSTS_IPM(ssts);
- return (det == AHCI_DET_COMM && ipm == AHCI_IPM_ACTIVE);
-}
-
-/*
- * Dump identify structure for debugging
- * purposes.
- */
-static void
-ahci_dump_identity(struct ata_identity *identity)
-{
- char serial_number[20];
- char model_number[40];
- char tmp;
-
- memcpy(serial_number, identity->serial_number, sizeof(serial_number));
- memcpy(model_number, identity->model_number, sizeof(model_number));
-
- serial_number[sizeof(serial_number) - 1] = '\0';
- model_number[sizeof(model_number) - 1] = '\0';
-
- /* Fixup endianess for serial number */
- for (size_t i = 0; i < sizeof(serial_number); i += 2) {
- tmp = serial_number[i];
- serial_number[i] = serial_number[i + 1];
- serial_number[i + 1] = tmp;
- }
-
- /* Fixup endianess for model number */
- for (size_t i = 0; i < sizeof(model_number); i += 2) {
- tmp = model_number[i];
- model_number[i] = model_number[i + 1];
- model_number[i + 1] = tmp;
- }
-
- pr_trace("DRIVE MODEL NUMBER: %s\n", model_number);
- pr_trace("DRIVE SERIAL NUMBER: %s\n", serial_number);
-}
-
-/*
- * Allocate a command slot.
- */
-static int
-ahci_alloc_cmdslot(struct ahci_hba *hba, struct hba_port *port)
-{
- uint32_t slotlist = (port->ci | port->sact);
-
- for (uint16_t i = 0; i < hba->ncmdslots; ++i) {
- if (!__TEST(slotlist, i))
- return i;
- }
-
- return -1;
-}
-
-/*
- * Submit a command to a device
- *
- * @port: Port of device to submit command to
- * @cmdslot: Command slot.
- */
-static int
-ahci_submit_cmd(struct ahci_hba *hba, struct hba_port *port, uint8_t cmdslot)
-{
- const uint32_t BUSY_BITS = (AHCI_PXTFD_BSY | AHCI_PXTFD_DRQ);
- const uint8_t MAX_ATTEMPTS = 3;
- uint8_t attempts = 0;
- int status = 0;
-
- /*
- * Ensure the port isn't busy before we try to send
- * any commands. Spin on BSY and DRQ bits until they
- * become unset or we timeout.
- */
- if (ahci_poll_reg32(&port->tfd, BUSY_BITS, false) < 0) {
- pr_error("Command failed: Port is busy! (slot=%d)\n", cmdslot);
- return -EBUSY;
- }
-
- /* Activate the command slot */
- mutex_acquire(&io_lock);
- mmio_write32(&port->ci, __BIT(cmdslot));
-
- /*
- * Wait for completion. since this might take a bit, we
- * give it a few attempts in case it doesn't finish
- * right away.
- */
- while ((attempts++) < MAX_ATTEMPTS) {
- status = ahci_poll_reg32(&port->ci, __BIT(cmdslot), false);
- if (status == 0) {
- break;
- }
- }
-
- /* Did we timeout? */
- if (status != 0) {
- pr_error("IDENTIFY timeout: slot %d still set!\n", cmdslot);
- }
-
- mutex_release(&io_lock);
- return status;
-}
-
-static int
-ahci_sata_rw(struct ahci_hba *hba, struct hba_port *port, struct sio_txn *sio,
- bool write)
-{
- paddr_t buf_phys;
- struct ahci_cmd_hdr *cmdhdr;
- struct ahci_cmdtab *cmdtbl;
- struct ahci_fis_h2d *fis;
- int cmdslot, status;
-
- if (sio->buf == NULL || !is_word_aligned(sio->buf))
- return -EINVAL;
- if (sio->len == 0)
- return -EINVAL;
-
- buf_phys = VIRT_TO_PHYS(sio->buf);
- cmdslot = ahci_alloc_cmdslot(hba, port);
-
- /* Setup command header */
- cmdhdr = PHYS_TO_VIRT(port->clb + cmdslot * sizeof(struct ahci_cmd_hdr));
- cmdhdr->w = 0;
- cmdhdr->cfl = sizeof(struct ahci_fis_h2d) / 4;
- cmdhdr->prdtl = 1;
-
- /* Setup physical region descriptor */
- cmdtbl = PHYS_TO_VIRT(cmdhdr->ctba);
- cmdtbl->prdt[0].dba = buf_phys;
- cmdtbl->prdt[0].dbc = (sio->len << 9) - 1;
- cmdtbl->prdt[0].i = 0;
-
- /* Setup command FIS */
- fis = (void *)&cmdtbl->cfis;
- fis->type = FIS_TYPE_H2D;
- fis->command = write ? ATA_CMD_WRITE_DMA : ATA_CMD_READ_DMA;
- fis->c = 1;
- fis->device = (1 << 6);
-
- /* Setup LBA */
- fis->lba0 = sio->offset & 0xFF;
- fis->lba1 = (sio->offset >> 8) & 0xFF;
- fis->lba2 = (sio->offset >> 16) & 0xFF;
- fis->lba3 = (sio->offset >> 24) & 0xFF;
- fis->lba4 = (sio->offset >> 32) & 0xFF;
- fis->lba5 = (sio->offset >> 40) & 0xFF;
-
- /* Setup count */
- fis->countl = sio->len & 0xFF;
- fis->counth = (sio->len >> 8) & 0xFF;
-
- if ((status = ahci_submit_cmd(hba, port, cmdslot)) != 0) {
- return status;
- }
-
- return 0;
-}
-
-/*
- * Send the IDENTIFY command to a device and
- * log info for debugging purposes.
- */
-static int
-ahci_identify(struct ahci_hba *hba, struct hba_port *port)
-{
- paddr_t buf_phys;
- struct ahci_cmd_hdr *cmdhdr;
- struct ahci_cmdtab *cmdtbl;
- struct ahci_fis_h2d *fis;
- int cmdslot;
- void *buf;
- int status = 0;
-
- cmdslot = ahci_alloc_cmdslot(hba, port);
- buf_phys = vm_alloc_pageframe(1);
- buf = PHYS_TO_VIRT(buf_phys);
-
- if (buf_phys == 0) {
- status = -ENOMEM;
- goto done;
- }
-
- if (cmdslot < 0) {
- status = cmdslot;
- goto done;
- }
-
- memset(buf, 0, vm_get_page_size());
- cmdhdr = PHYS_TO_VIRT(port->clb + cmdslot * sizeof(struct ahci_cmd_hdr));
- cmdhdr->w = 0;
- cmdhdr->cfl = sizeof(struct ahci_fis_h2d) / 4;
- cmdhdr->prdtl = 1;
-
- cmdtbl = PHYS_TO_VIRT(cmdhdr->ctba);
- cmdtbl->prdt[0].dba = VIRT_TO_PHYS(buf);
- cmdtbl->prdt[0].dbc = 511;
- cmdtbl->prdt[0].i = 0;
-
- fis = (void *)&cmdtbl->cfis;
- fis->command = ATA_CMD_IDENTIFY;
- fis->c = 1;
- fis->type = FIS_TYPE_H2D;
-
- if ((status = ahci_submit_cmd(hba, port, cmdslot)) != 0) {
- goto done;
- }
-
- ahci_dump_identity(buf);
-done:
- vm_free_pageframe(VIRT_TO_PHYS(buf), 1);
- return status;
-}
-
-/*
- * Device interface read/write helper
- */
-static int
-sata_dev_rw(struct device *dev, struct sio_txn *sio, bool write)
-{
- struct ahci_device *sata;
-
- if (sio == NULL)
- return -1;
- if (sio->buf == NULL)
- return -1;
-
- sata = ahci_get_sata(dev->minor);
-
- if (sata == NULL)
- return -1;
-
- return ahci_sata_rw(sata->hba, sata->port, sio, write);
-}
-
-/*
- * Device interface read
- */
-static int
-sata_dev_read(struct device *dev, struct sio_txn *sio)
-{
- return sata_dev_rw(dev, sio, false);
-}
-
-/*
- * Device interface write
- */
-static int
-sata_dev_write(struct device *dev, struct sio_txn *sio)
-{
- return sata_dev_rw(dev, sio, true);
-}
-
-/*
- * Device interface open
- */
-static int
-sata_dev_open(struct device *dev)
-{
- return 0;
-}
-
-/*
- * Device interface close
- */
-static int
-sata_dev_close(struct device *dev)
-{
- return 0;
-}
-
-/*
- * Register a SATA device to the rest of the system
- * and expose to userland as a device file.
- */
-static int
-ahci_sata_register(struct ahci_hba *hba, struct hba_port *port)
-{
- char devname[128];
- struct device *dev = NULL;
- struct ahci_device *sata = NULL;
- dev_t dev_id;
- dev_t major;
-
- sata = dynalloc(sizeof(struct ahci_device));
- if (sata == NULL) {
- return -ENOMEM;
- }
-
- dev_id = ++dev_count;
- major = device_alloc_major();
-
- dev = device_alloc();
- dev->open = sata_dev_open;
- dev->close = sata_dev_close;
- dev->read = sata_dev_read;
- dev->write = sata_dev_write;
- dev->blocksize = 512;
- device_create(dev, dev_id, major);
-
- sata->port = port;
- sata->hba = hba;
- sata->minor = dev->minor;
-
- snprintf(devname, sizeof(devname), "sd%d", dev_id);
- devfs_add_dev(devname, dev);
- TAILQ_INSERT_TAIL(&sata_devs, sata, link);
- return 0;
-}
-
-/*
- * Init a single port.
- *
- * @port: Port to init.
- */
-static int
-ahci_init_port(struct ahci_hba *hba, struct hba_port *port, size_t portno)
-{
- paddr_t tmp;
- struct ahci_cmd_hdr *cmdlist;
- void *fis;
- size_t cmdlist_size, pagesize;
- uint32_t sig;
- uint8_t ncmdslots;
- int status = 0;
-
- sig = mmio_read32(&port->sig);
- status = ahci_stop_port(port);
- if (status != 0) {
- pr_trace("Failed to stop port %d\n", portno);
- return status;
- }
-
- /* Try to report device type based on signature */
- switch (sig) {
- case AHCI_SIG_PM:
- pr_trace("Port %d has port multiplier signature\n", portno);
- return 0; /* TODO */
- case AHCI_SIG_ATA:
- pr_trace("Port %d has ATA signature (SATA drive)\n", portno);
- break;
- default:
- return 0; /* TODO */
- }
-
- ncmdslots = hba->ncmdslots;
- pagesize = vm_get_page_size();
-
- /* Allocate our command list */
- cmdlist_size = __ALIGN_UP(ncmdslots * AHCI_CMDENTRY_SIZE, pagesize);
- tmp = vm_alloc_pageframe(cmdlist_size / pagesize);
- cmdlist = PHYS_TO_VIRT(tmp);
- if (tmp == 0) {
- pr_trace("Failed to allocate cmdlist\n");
- status = -ENOMEM;
- goto done;
- }
-
- tmp = vm_alloc_pageframe(1);
- fis = PHYS_TO_VIRT(tmp);
- if (tmp == 0) {
- pr_trace("Failed to allocate FIS\n");
- status = -ENOMEM;
- goto done;
- }
-
- memset(cmdlist, 0, cmdlist_size);
- memset(fis, 0, AHCI_FIS_SIZE);
- hba->cmdlist = cmdlist;
-
- /* Set the registers */
- port->clb = VIRT_TO_PHYS(cmdlist);
- port->fb = VIRT_TO_PHYS(fis);
-
- for (int i = 0; i < ncmdslots; ++i) {
- cmdlist[i].prdtl = 1;
- cmdlist[i].ctba = vm_alloc_pageframe(1);
- }
-
- /* Now try to start up the port */
- if ((status = ahci_start_port(port)) != 0) {
- pr_trace("Failed to start port %d\n", portno);
- goto done;
- }
-
- ahci_identify(hba, port);
- ahci_sata_register(hba, port);
-done:
- if (status != 0 && cmdlist != NULL)
- vm_free_pageframe(port->clb, cmdlist_size / pagesize);
- if (status != 0 && fis != NULL)
- vm_free_pageframe(port->fb, 1);
-
- return status;
-}
-
-/*
- * Hard reset port and reinitialize
- * link.
- */
-static int
-ahci_reset_port(struct hba_port *port)
-{
- uint32_t sctl, ssts;
-
- /*
- * Some odd behaviour may occur if a COMRESET is sent
- * to the port while it is in an idle state...
- * A workaround to this is to bring the port up
- * then immediately transmit the COMRESET to the device.
- */
- ahci_start_port(port);
- sctl = mmio_read32(&port->sctl);
-
- /* Transmit COMRESET for ~2ms */
- sctl = (sctl & ~0xF) | AHCI_DET_COMRESET;
- mmio_write32(&port->sctl, sctl);
- driver_tmr.msleep(2);
-
- /* Stop transmission of COMRESET */
- sctl &= ~AHCI_DET_COMRESET;
- mmio_write32(&port->sctl, sctl);
-
- /*
- * Give around ~150ms for the link to become
- * reestablished. Then make sure that it is
- * actually established by checking PxSSTS.DET
- */
- driver_tmr.msleep(150);
- ssts = mmio_read32(&port->ssts);
- if (AHCI_PXSSTS_DET(ssts) != AHCI_DET_COMM) {
- return -1;
- }
-
- return 0;
-}
-
-/*
- * Sets up devices connected to the physical ports
- * on the HBA.
- *
- * XXX: Since this is called after ahci_init_hba() which also
- * resets the HBA, we'll need to reestablish the link
- * between the devices and the HBA.
- */
-static int
-ahci_init_ports(struct ahci_hba *hba)
-{
- struct hba_memspace *abar = hba->abar;
- uint32_t ports_impl;
- struct hba_port *port;
-
- pr_trace("HBA supports max %d port(s)\n", hba->nports);
- ports_impl = mmio_read32(&abar->pi);
-
- /* Initialize active ports */
- for (size_t i = 0; i < sizeof(abar->pi) * 8; ++i) {
- if (!__TEST(ports_impl, __BIT(i))) {
- continue;
- }
-
- port = &abar->ports[i];
- if (ahci_reset_port(port) != 0) {
- continue;
- }
-
- if (ahci_port_active(port)) {
- ahci_init_port(hba, port, i);
- }
- }
-
- return 0;
-}
-
-/*
- * Sets up the HBA
- */
-static int
-ahci_init_hba(struct ahci_hba *hba)
-{
- struct hba_memspace *abar = hba->abar;
- uint32_t cap;
-
- /* Reset the HBA to ensure it is a known state */
- ahci_hba_reset(hba);
-
- /* Setup HBA structure and save some state */
- cap = mmio_read32(&abar->cap);
- hba->ncmdslots = AHCI_CAP_NCS(cap) + 1;
- hba->nports = AHCI_CAP_NP(cap) + 1;
-
- ahci_init_ports(hba);
- return 0;
-}
-
-static int
-ahci_init(void)
-{
- int status;
- uint16_t cmdreg_bits;
- uint32_t bar_size;
- struct ahci_hba hba = {0};
- struct pci_lookup ahci_lookup = {
- .pci_class = 0x01,
- .pci_subclass = 0x06
- };
-
- dev = pci_get_device(ahci_lookup, PCI_CLASS | PCI_SUBCLASS);
-
- if (dev == NULL) {
- return -1;
- }
-
- cmdreg_bits = PCI_BUS_MASTERING | PCI_MEM_SPACE;
- pci_set_cmdreg(dev, cmdreg_bits);
-
- if (req_timer(TIMER_GP, &driver_tmr) != TMRR_SUCCESS) {
- pr_error("Failed to fetch general purpose timer\n");
- return -1;
- }
-
- if (driver_tmr.msleep == NULL) {
- pr_error("Timer does not have msleep()\n");
- return -1;
- }
-
- if ((bar_size = pci_bar_size(dev, 5)) == 0) {
- pr_error("Failed to fetch BAR size\n");
- return -1;
- }
-
- status = bus_map(dev->bar[5], bar_size, 0, (void *)&hba.abar);
- if (status != 0) {
- pr_error("Failed to map BAR into higher half\n");
- return -1;
- }
-
- pr_trace("AHCI HBA memspace @ 0x%p\n", hba.abar);
- TAILQ_INIT(&sata_devs);
- ahci_init_hba(&hba);
- return 0;
-}
-
-DRIVER_EXPORT(ahci_init);
diff --git a/sys/dev/ic/nvme.c b/sys/dev/ic/nvme.c
deleted file mode 100644
index df533a3..0000000
--- a/sys/dev/ic/nvme.c
+++ /dev/null
@@ -1,614 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/driver.h>
-#include <sys/cdefs.h>
-#include <sys/syslog.h>
-#include <sys/timer.h>
-#include <sys/device.h>
-#include <dev/pci/pci.h>
-#include <dev/ic/nvmevar.h>
-#include <vm/dynalloc.h>
-#include <vm/vm.h>
-#include <fs/devfs.h>
-#include <string.h>
-
-__MODULE_NAME("nvme");
-__KERNEL_META("$Hyra$: nvme.c, Ian Marco Moffett, "
- "NVMe driver");
-
-#define pr_trace(fmt, ...) kprintf("nvme: " fmt, ##__VA_ARGS__)
-#define pr_error(...) pr_trace(__VA_ARGS__)
-
-static struct pci_device *nvme_dev;
-static struct timer driver_tmr;
-static TAILQ_HEAD(,nvme_ns) namespaces;
-
-static inline int
-is_4k_aligned(void *ptr)
-{
- return ((uintptr_t)ptr & (0x1000 - 1)) == 0;
-}
-
-/*
- * Poll CSTS.RDY to equal `val'
- *
- * Returns `val' on success, returns < 0 value
- * upon failure.
- */
-static int
-nvme_poll_ready(struct nvme_bar *bar, uint8_t val)
-{
- uint8_t timeout = CAP_TIMEOUT(bar->caps);
- uint8_t time_waited = 0;
-
- do {
- if (STATUS_READY(bar->status) == val) {
- /* Done waiting */
- break;
- }
-
- /*
- * If CSTS.RDY hasn't changed, we can try to wait a
- * little longer.
- *
- * XXX: The spec states that CAP.TO (Timeout) is in 500
- * millisecond units.
- */
- if (time_waited < timeout) {
- driver_tmr.msleep(500);
- ++time_waited;
- } else {
- return -1;
- }
- } while (1);
-
- return val;
-}
-
-/*
- * Create an NVMe queue.
- */
-static int
-nvme_create_queue(struct nvme_state *s, struct nvme_queue *queue, size_t id)
-{
- struct nvme_bar *bar = s->bar;
- const size_t PAGESZ = vm_get_page_size();
- const uint8_t DBSTRIDE = CAP_STRIDE(bar->caps);
- const uint16_t SLOTS = CAP_MQES(bar->caps);
-
- queue->sq = dynalloc_memalign(sizeof(void *) * SLOTS, 0x1000);
- queue->cq = dynalloc_memalign(sizeof(void *) * SLOTS, 0x1000);
-
- if (queue->sq == NULL) {
- return -1;
- }
- if (queue->cq == NULL) {
- dynfree(queue->sq);
- return -1;
- }
-
- memset(queue->sq, 0, sizeof(void *) * SLOTS);
- memset(queue->cq, 0, sizeof(void *) * SLOTS);
-
- queue->sq_head = 0;
- queue->sq_tail = 0;
- queue->size = SLOTS;
- queue->sq_db = PHYS_TO_VIRT((uintptr_t)bar + PAGESZ + (2 * id * (4 << DBSTRIDE)));
- queue->cq_db = PHYS_TO_VIRT((uintptr_t)bar + PAGESZ + ((2 * id + 1) * (4 << DBSTRIDE)));
- queue->cq_phase = 1;
- return 0;
-}
-
-/*
- * Submit a command
- *
- * @queue: Target queue.
- * @cmd: Command to submit
- */
-static void
-nvme_submit_cmd(struct nvme_queue *queue, struct nvme_cmd cmd)
-{
- /* Submit the command to the queue */
- queue->sq[queue->sq_tail++] = cmd;
- if (queue->sq_tail >= queue->size) {
- queue->sq_tail = 0;
- }
- *(queue->sq_db) = queue->sq_tail;
-}
-
-/*
- * Submit a command and poll for completion
- *
- * @queue: Target queue.
- * @cmd: Command to submit
- */
-static int
-nvme_poll_submit_cmd(struct nvme_queue *queue, struct nvme_cmd cmd)
-{
- uint16_t status;
- size_t spins = 0;
-
- nvme_submit_cmd(queue, cmd);
-
- /*
- * Wait for the current command to complete by
- * polling the phase bit.
- */
- while (1) {
- status = queue->cq[queue->cq_head].status;
- if ((status & 1) == queue->cq_phase) {
- /*
- * The phase bit matches the phase for the most
- * recently submitted command, the command has completed.
- */
- break;
- }
- if ((status & ~1) != 0) {
- pr_trace("NVMe cmd error (bits=0x%x)\n", status >> 1);
- break;
- }
- if (spins > 5) {
- /* Attempts exhausted */
- pr_error("Hang on phase bit poll, giving up (cmd error)\n");
- break;
- }
-
- /* Not done, give it some more time */
- driver_tmr.msleep(150);
- ++spins;
- }
-
- ++queue->cq_head;
- if (queue->cq_head >= queue->size) {
- queue->cq_head = 0;
- queue->cq_phase = !queue->cq_phase;
- }
-
- /* Tell the controller that `head' updated */
- *(queue->cq_db) = queue->cq_head;
- return 0;
-}
-
-/*
- * Create an I/O queue for a specific namespace.
- *
- * @ns: Namespace
- * @id: I/O queue ID
- */
-static int
-nvme_create_ioq(struct nvme_ns *ns, size_t id)
-{
- struct nvme_queue *ioq = &ns->ioq;
- struct nvme_state *cntl = ns->cntl;
-
- struct nvme_bar *bar = cntl->bar;
- struct nvme_cmd cmd = {0};
- size_t mqes = CAP_MQES(bar->caps);
-
- struct nvme_create_iocq_cmd *create_iocq;
- struct nvme_create_iosq_cmd *create_iosq;
- int status;
-
- if ((status = nvme_create_queue(ns->cntl, ioq, id)) != 0) {
- return status;
- }
-
- create_iocq = &cmd.create_iocq;
- create_iocq->opcode = NVME_OP_CREATE_IOCQ;
- create_iocq->qflags |= __BIT(0); /* Physically contiguous */
- create_iocq->qsize = mqes;
- create_iocq->qid = id;
- create_iocq->prp1 = VIRT_TO_PHYS(ns->ioq.cq);
-
- if ((status = nvme_poll_submit_cmd(&cntl->adminq, cmd)) != 0) {
- return status;
- }
-
- create_iosq = &cmd.create_iosq;
- create_iosq->opcode = NVME_OP_CREATE_IOSQ;
- create_iosq->qflags |= __BIT(0); /* Physically contiguous */
- create_iosq->qsize = mqes;
- create_iosq->cqid = id;
- create_iosq->sqid = id;
- create_iosq->prp1 = VIRT_TO_PHYS(ns->ioq.sq);
- return nvme_poll_submit_cmd(&cntl->adminq, cmd);
-}
-
-/*
- * Issue an identify command for the current
- * controller.
- *
- * XXX: `id' must be aligned on a 4k byte boundary to avoid
- * crossing a page boundary. This keeps the implementation
- * as simple as possible here.
- */
-static int
-nvme_identify(struct nvme_state *state, struct nvme_id *id)
-{
- struct nvme_cmd cmd = {0};
- struct nvme_identify_cmd *identify = &cmd.identify;
-
- /* Ensure `id' is aligned on a 4k byte boundary */
- if (!is_4k_aligned(id)) {
- return -1;
- }
-
- identify->opcode = NVME_OP_IDENTIFY;
- identify->nsid = 0;
- identify->cns = 1; /* Identify controller */
- identify->prp1 = VIRT_TO_PHYS(id);
- identify->prp2 = 0; /* No need, data address is 4k aligned */
- return nvme_poll_submit_cmd(&state->adminq, cmd);
-}
-
-/*
- * Issue a read/write command for a specific
- * namespace.
- *
- * `buf' must be 4k aligned.
- */
-static int
-nvme_rw(struct nvme_ns *ns, char *buf, off_t slba, size_t count, bool write)
-{
- struct nvme_cmd cmd = {0};
- struct nvme_rw_cmd *rw = &cmd.rw;
-
- if (!is_4k_aligned(buf)) {
- return -1;
- }
-
- rw->opcode = write ? NVME_OP_WRITE : NVME_OP_READ;
- rw->nsid = ns->nsid;
- rw->slba = slba;
- rw->len = count - 1;
- rw->prp1 = VIRT_TO_PHYS(buf);
- return nvme_poll_submit_cmd(&ns->ioq, cmd);
-}
-
-/*
- * Fetch a namespace from its ID
- *
- * @nsid: Namespace ID of namespace to fetch
- */
-static struct nvme_ns *
-nvme_get_ns(size_t nsid)
-{
- struct nvme_ns *ns;
-
- TAILQ_FOREACH(ns, &namespaces, link) {
- if (ns->nsid == nsid) {
- return ns;
- }
- }
-
- return NULL;
-}
-
-/*
- * Device interface read/write helper
- */
-static int
-nvme_dev_rw(struct device *dev, struct sio_txn *sio, bool write)
-{
- struct nvme_ns *ns;
-
- if (sio == NULL) {
- return -1;
- }
-
- ns = nvme_get_ns(dev->minor);
- if (ns == NULL || sio->buf == NULL) {
- return -1;
- }
-
- return nvme_rw(ns, sio->buf, sio->offset, sio->len, write);
-}
-
-/*
- * Device interface read
- */
-static int
-nvme_dev_read(struct device *dev, struct sio_txn *sio)
-{
- return nvme_dev_rw(dev, sio, false);
-}
-
-/*
- * Device interface write
- */
-static int
-nvme_dev_write(struct device *dev, struct sio_txn *sio)
-{
- return nvme_dev_rw(dev, sio, true);
-}
-
-static int
-nvme_dev_open(struct device *dev)
-{
- return 0;
-}
-
-/*
- * Get identify data for namespace
- *
- * @id_ns: Data will be written to this pointer via DMA.
- * @nsid: Namespace ID.
- *
- * XXX: `id_ns' must be 4k aligned.
- */
-static int
-nvme_id_ns(struct nvme_state *s, struct nvme_id_ns *id_ns, uint16_t nsid)
-{
- struct nvme_cmd cmd = {0};
- struct nvme_identify_cmd *identify = &cmd.identify;
-
- if (!is_4k_aligned(id_ns)) {
- return -1;
- }
-
- identify->opcode = NVME_OP_IDENTIFY;
- identify->nsid = nsid;
- identify->cns = 0;
- identify->prp1 = VIRT_TO_PHYS(id_ns);
- return nvme_poll_submit_cmd(&s->adminq, cmd);
-}
-
-/*
- * Init a namespace.
- *
- * @nsid: Namespace ID
- */
-static int
-nvme_init_ns(struct nvme_state *state, uint16_t nsid)
-{
- char devname[128];
- struct nvme_ns *ns = NULL;
- struct nvme_id_ns *id_ns = NULL;
- struct device *dev;
- uint8_t lba_format;
- int status = 0;
-
- ns = dynalloc(sizeof(struct nvme_ns));
- if (ns == NULL) {
- status = -1;
- goto done;
- }
-
- id_ns = dynalloc_memalign(sizeof(struct nvme_id_ns), 0x1000);
- if ((status = nvme_id_ns(state, id_ns, nsid)) != 0) {
- dynfree(ns);
- goto done;
- }
-
- lba_format = id_ns->flbas & 0xF;
- ns->lba_fmt = id_ns->lbaf[lba_format];
- ns->nsid = nsid;
- ns->lba_bsize = 1 << ns->lba_fmt.ds;
- ns->size = id_ns->size;
- ns->cntl = state;
- nvme_create_ioq(ns, ns->nsid);
-
- dev = device_alloc();
- dev->read = nvme_dev_read;
- dev->write = nvme_dev_write;
- dev->open = nvme_dev_open;
- dev->blocksize = ns->lba_bsize;
- dev->mmap = NULL;
- ns->dev_id = device_create(dev, state->major, nsid);
-
- snprintf(devname, sizeof(devname), "nvme0n%d", nsid);
- if (devfs_add_dev(devname, dev) != 0) {
- pr_error("Failed to create /dev/%s\n", devname);
- }
-
- TAILQ_INSERT_TAIL(&namespaces, ns, link);
-done:
- if (id_ns != NULL)
- dynfree(id_ns);
-
- return status;
-}
-
-static int
-nvme_disable_controller(struct nvme_state *state)
-{
- struct nvme_bar *bar = state->bar;
-
- if (__TEST(bar->config, CONFIG_EN)) {
- bar->config &= ~CONFIG_EN;
- }
-
- if (nvme_poll_ready(bar, 0) < 0) {
- pr_error("Failed to disable controller\n");
- return -1;
- }
-
- return 0;
-}
-
-/*
- * For debugging purposes, logs some information
- * found within the controller identify data structure.
- */
-static void
-nvme_log_ctrl_id(struct nvme_id *id)
-{
- char mn[41] = {0};
- char fr[9] = {0};
-
- for (size_t i = 0; i < sizeof(id->mn); ++i) {
- mn[i] = id->mn[i];
- }
- for (size_t i = 0; i < sizeof(id->fr); ++i) {
- fr[i] = id->fr[i];
- }
-
- pr_trace("NVMe model: %s\n", mn);
- pr_trace("NVMe firmware revision: %s\n", fr);
-}
-
-/*
- * Fetch the list of namespace IDs
- *
- * @nsids_out: NSIDs will be written here via DMA.
- *
- * XXX: `nsids_out' must be 4k aligned.
- */
-static int
-nvme_get_nsids(struct nvme_state *state, uint32_t *nsids_out)
-{
- struct nvme_cmd cmd = {0};
- struct nvme_identify_cmd *identify = &cmd.identify;
-
- if (!is_4k_aligned(nsids_out)) {
- return -1;
- }
-
- identify->opcode = NVME_OP_IDENTIFY;
- identify->cns = 2; /* Active NSID list */
- identify->prp1 = VIRT_TO_PHYS(nsids_out);
- return nvme_poll_submit_cmd(&state->adminq, cmd);
-}
-
-static int
-nvme_enable_controller(struct nvme_state *state)
-{
- struct nvme_bar *bar = state->bar;
- struct nvme_id *id;
-
- uint32_t *nsids;
- uint8_t max_sqes, max_cqes;
-
- if (!__TEST(bar->config, CONFIG_EN)) {
- bar->config |= CONFIG_EN;
- }
-
- if (nvme_poll_ready(bar, 1) < 0) {
- pr_error("Failed to enable controller\n");
- return -1;
- }
-
- id = dynalloc_memalign(sizeof(struct nvme_id), 0x1000);
- if (id == NULL) {
- return -1;
- }
-
- nsids = dynalloc_memalign(0x1000, 0x1000);
- if (nsids == NULL) {
- return -1;
- }
-
- nvme_identify(state, id);
- nvme_log_ctrl_id(id);
- nvme_get_nsids(state, nsids);
-
- /*
- * Before creating any I/O queues we need to set CC.IOCQES
- * and CC.IOSQES... Bits 3:0 is the minimum and bits 7:4
- * is the maximum. We'll choose the maximum.
- */
- max_sqes = id->sqes >> 4;
- max_cqes = id->cqes >> 4;
- bar->config |= (max_sqes << CONFIG_IOSQES_SHIFT);
- bar->config |= (max_cqes << CONFIG_IOCQES_SHIFT);
-
- /* Init NVMe namespaces */
- for (size_t i = 0; i < id->nn; ++i) {
- if (nsids[i] != 0) {
- pr_trace("Found NVMe namespace (id=%d)\n", nsids[i]);
- nvme_init_ns(state, nsids[i]);
- }
- }
-
- dynfree(nsids);
- dynfree(id);
- return 0;
-}
-
-static int
-nvme_init_controller(struct nvme_bar *bar)
-{
- struct nvme_state state = { . bar = bar };
- struct nvme_queue *adminq = &state.adminq;
-
- uint16_t mqes = CAP_MQES(bar->caps);
- uint16_t cmdreg_bits = PCI_BUS_MASTERING |
- PCI_MEM_SPACE;
-
- pci_set_cmdreg(nvme_dev, cmdreg_bits);
- nvme_disable_controller(&state);
-
- nvme_create_queue(&state, adminq, 0);
-
- /* Setup admin submission and admin completion queues */
- bar->aqa = (mqes | mqes << 16);
- bar->asq = VIRT_TO_PHYS(adminq->sq);
- bar->acq = VIRT_TO_PHYS(adminq->cq);
-
- state.major = device_alloc_major();
- return nvme_enable_controller(&state);
-}
-
-static int
-nvme_init(void)
-{
- struct nvme_bar *bar;
- struct pci_lookup nvme_lookup = {
- .pci_class = 1,
- .pci_subclass = 8
- };
-
- if (req_timer(TIMER_GP, &driver_tmr) != 0) {
- pr_error("Failed to fetch general purpose timer\n");
- return -1;
- }
-
- if (driver_tmr.msleep == NULL) {
- pr_error("Timer does not have msleep()\n");
- return -1;
- }
-
- nvme_dev = pci_get_device(nvme_lookup, PCI_CLASS | PCI_SUBCLASS);
- if (nvme_dev == NULL) {
- return -1;
- }
-
- bar = PCI_BAR_MEMBASE(nvme_dev->bar[0]);
- pr_trace("NVMe BAR0 @ 0x%p\n", bar);
- TAILQ_INIT(&namespaces);
-
- if (nvme_init_controller(bar) < 0) {
- return -1;
- }
-
- return 0;
-}
-
-DRIVER_EXPORT(nvme_init);
diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c
deleted file mode 100644
index 9a8eae4..0000000
--- a/sys/dev/pci/pci.c
+++ /dev/null
@@ -1,344 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <dev/pci/pci.h>
-#include <dev/pci/pcivar.h>
-#include <sys/cdefs.h>
-#include <sys/panic.h>
-#include <sys/queue.h>
-#include <sys/syslog.h>
-#include <sys/errno.h>
-#include <vm/dynalloc.h>
-#include <machine/bus.h>
-#if defined(__x86_64__)
-#include <machine/io.h>
-#endif
-#include <assert.h>
-
-__MODULE_NAME("pci");
-__KERNEL_META("$Hyra$: pci.c, Ian Marco Moffett, "
- "PCI driver core");
-
-#define pr_trace(fmt, ...) kprintf("pci: " fmt, ##__VA_ARGS__)
-
-static TAILQ_HEAD(, pci_device) device_list;
-static int access_method = PCI_ACCESS_CAM;
-
-/*
- * Read device's legacy PCI CAM space
- *
- * @dev: Device to read.
- * @offset: Offset to read at.
- *
- * XXX: Do not use directly!
- */
-static uint32_t
-pci_cam_read(const struct pci_device *dev, uint32_t offset)
-{
-#if defined(__x86_64__)
- uint32_t address, data;
-
- address = __BIT(31) |
- (offset & ~3) |
- (dev->func << 8) |
- (dev->slot << 11) |
- (dev->bus << 16);
-
- outl(0xCF8, address);
- data = inl(0xCFC) >> ((offset & 3) * 8);
- return data;
-#else
- panic("Invalid arch (%s())\n", __func__);
-#endif
-}
-
-/*
- * Write to device's legacy PCI CAM space
- *
- * @dev: Device to write to.
- * @offset: Offset to write at.
- *
- * XXX: Do not use directly!
- */
-static void
-pci_cam_write(const struct pci_device *dev, uint32_t offset, uint32_t value)
-{
-#if defined(__x86_64__)
- uint32_t address;
-
- address = __BIT(31) |
- (offset & ~3) |
- (dev->func << 8) |
- (dev->slot << 11) |
- (dev->bus << 16);
-
- outl(0xCF8, address);
- outb(0xCFC, value);
-#else
- panic("Invalid arch (%s())\n", __func__);
-#endif
-}
-
-static bool
-pci_device_exists(uint8_t bus, uint8_t slot, uint8_t func)
-{
- uint16_t vendor_id;
- struct pci_device dev_tmp = {
- .bus = bus,
- .slot = slot,
- .func = func
- };
-
- vendor_id = pci_cam_read(&dev_tmp, 0x0) & 0xFFFF;
-
- if (vendor_id == 0xFFFF) {
- return false;
- }
-
- return true;
-}
-
-/*
- * Sets other device information e.g., device id, vendor id, etc
- *
- * @dev: Device descriptor to set up.
- *
- * XXX: Expects device bus, slot and func to be set.
- */
-static void
-pci_set_device_info(struct pci_device *dev)
-{
- uint32_t classrev;
-
- dev->vendor_id = pci_readl(dev, PCIREG_VENDOR_ID) & __MASK(16);
- dev->device_id = pci_readl(dev, PCIREG_DEVICE_ID) & __MASK(16);
- classrev = pci_readl(dev, PCIREG_CLASSREV);
-
- dev->pci_class = PCIREG_CLASS(classrev);
- dev->pci_subclass = PCIREG_SUBCLASS(classrev);
- dev->prog_if = PCIREG_PROGIF(classrev);
-
- dev->bar[0] = pci_readl(dev, PCIREG_BAR0);
- dev->bar[1] = pci_readl(dev, PCIREG_BAR1);
- dev->bar[2] = pci_readl(dev, PCIREG_BAR2);
- dev->bar[3] = pci_readl(dev, PCIREG_BAR3);
- dev->bar[4] = pci_readl(dev, PCIREG_BAR4);
- dev->bar[5] = pci_readl(dev, PCIREG_BAR5);
-
- dev->irq_line = pci_readl(dev, PCIREG_IRQLINE) & __MASK(8);
-}
-
-static void
-pci_register_device(uint8_t bus, uint8_t slot, uint8_t func)
-{
- struct pci_device *dev = NULL;
-
- if (!pci_device_exists(bus, slot, func)) {
- return;
- }
-
- dev = dynalloc(sizeof(struct pci_device));
- __assert(dev != NULL);
-
- dev->bus = bus;
- dev->slot = slot;
- dev->func = func;
-
- pci_set_device_info(dev);
- TAILQ_INSERT_TAIL(&device_list, dev, link);
-}
-
-static void
-pci_scan_bus(uint8_t bus)
-{
- for (int slot = 0; slot < 32; ++slot) {
- for (int func = 0; func < 8; ++func) {
- pci_register_device(bus, slot, func);
- }
- }
-}
-
-/*
- * Convert a BAR number to BAR register offset.
- *
- * @dev: Device of BAR to check.
- * @bar: Bar number.
- */
-static uint8_t
-pci_get_barreg(struct pci_device *dev, uint8_t bar)
-{
- switch (bar) {
- case 0: return PCIREG_BAR0;
- case 1: return PCIREG_BAR1;
- case 2: return PCIREG_BAR2;
- case 3: return PCIREG_BAR3;
- case 4: return PCIREG_BAR4;
- case 5: return PCIREG_BAR5;
- default: return 0;
- }
-}
-
-/*
- * Get size length of memory region that a PCI(e) BAR
- * covers. A returned value of zero is invalid and indicates
- * an error.
- *
- * @dev: Device of BAR to get.
- * @bar: BAR number.
- */
-uint32_t
-pci_bar_size(struct pci_device *dev, uint8_t bar)
-{
- uint8_t bar_reg = pci_get_barreg(dev, bar);
- uint32_t tmp, size;
-
- if (bar_reg == 0) {
- return 0;
- }
-
- /*
- * Get the length of the region this BAR covers by writing a
- * mask of 32 bits into the BAR register and seeing how many
- * bits are unset. We can use this to compute the size of the
- * region. We know that log2(len) bits must be unset.
- */
- tmp = pci_readl(dev, bar_reg);
- pci_writel(dev, bar_reg, __MASK(32));
- size = pci_readl(dev, bar_reg);
- size = ~size + 1;
-
- /* Now we need to restore the previous value */
- pci_writel(dev, bar_reg, tmp);
- return size;
-}
-
-/*
- * Read PCI(e) configuration space.
- *
- * @dev: Device to read from.
- * @offset: Offset to read at.
- */
-uint32_t
-pci_readl(struct pci_device *dev, uint32_t offset)
-{
- if (access_method == PCI_ACCESS_CAM) {
- return pci_cam_read(dev, offset);
- }
-
- panic("Invalid access method (%s())\n", __func__);
- __builtin_unreachable();
-}
-
-/*
- * Write to PCI(e) configuration space.
- *
- * @dev: Device to write to.
- * @offset: Offset to write at.
- */
-void
-pci_writel(struct pci_device *dev, uint32_t offset, uint32_t val)
-{
- if (access_method == PCI_ACCESS_CAM) {
- pci_cam_write(dev, offset, val);
- return;
- }
-
- panic("Invalid access method (%s())\n", __func__);
- __builtin_unreachable();
-}
-
-/*
- * Set command register bits.
- *
- * @dev: Device whose command register to modify.
- * @bits: Bits to set.
- */
-void
-pci_set_cmdreg(struct pci_device *dev, uint16_t bits)
-{
- uint32_t tmp;
-
- tmp = pci_readl(dev, 0x4) | bits;
- pci_writel(dev, 0x4, tmp);
-}
-
-struct pci_device *
-pci_get_device(struct pci_lookup lookup, uint16_t lookup_type)
-{
- struct pci_device *dev;
- uint16_t lookup_matches = 0;
-
- TAILQ_FOREACH(dev, &device_list, link) {
- if (__TEST(lookup_type, PCI_DEVICE_ID)) {
- /* Check device ID */
- if (lookup.device_id == dev->device_id)
- lookup_matches |= PCI_DEVICE_ID;
- }
-
- if (__TEST(lookup_type, PCI_VENDOR_ID)) {
- /* Check vendor ID */
- if (lookup.vendor_id == dev->vendor_id)
- lookup_matches |= PCI_VENDOR_ID;
- }
-
- if (__TEST(lookup_type, PCI_CLASS)) {
- /* Check PCI class */
- if (lookup.pci_class == dev->pci_class)
- lookup_matches |= PCI_CLASS;
- }
-
- if (__TEST(lookup_type, PCI_SUBCLASS)) {
- /* Check PCI subclass */
- if (lookup.pci_subclass == dev->pci_subclass)
- lookup_matches |= PCI_SUBCLASS;
- }
-
- if (lookup_type == lookup_matches) {
- /* We found the device! */
- return dev;
- }
- lookup_matches = 0;
- }
-
- return NULL;
-}
-
-int
-pci_init(void)
-{
- TAILQ_INIT(&device_list);
-
- pr_trace("Scanning each bus...\n");
-
- for (uint16_t i = 0; i < 256; ++i) {
- pci_scan_bus(i);
- }
-
- return 0;
-}
diff --git a/sys/dev/usb/xhci.c b/sys/dev/usb/xhci.c
deleted file mode 100644
index f221843..0000000
--- a/sys/dev/usb/xhci.c
+++ /dev/null
@@ -1,463 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/driver.h>
-#include <dev/pci/pci.h>
-#include <sys/syslog.h>
-#include <sys/timer.h>
-#include <dev/usb/xhciregs.h>
-#include <dev/usb/xhcivar.h>
-#include <vm/physseg.h>
-#include <vm/dynalloc.h>
-#include <vm/vm.h>
-#include <string.h>
-#include <assert.h>
-
-__MODULE_NAME("xhci");
-__KERNEL_META("$Hyra$: xhci.c, Ian Marco Moffett, "
- "xHCI driver");
-
-#define pr_trace(fmt, ...) kprintf("xhci: " fmt, ##__VA_ARGS__)
-#define pr_error(...) pr_trace(__VA_ARGS__)
-
-static struct pci_device *hci_dev;
-static struct timer driver_tmr;
-
-static inline uint32_t *
-xhci_get_portsc(struct xhci_hc *hc, uint8_t portno)
-{
- if (portno > hc->maxports) {
- portno = hc->maxports;
- }
- return XHCI_BASE_OFF(hc->opregs, 0x400 + (0x10 * (portno - 1)));
-}
-
-/*
- * Set event ring segment table base
- * address.
- *
- * @hc: Host controler descriptor.
- * @pa: Physical address.
- */
-static inline void
-xhci_set_erst_base(struct xhci_hc *hc, uintptr_t pa)
-{
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
- void *runtime_base = XHCI_RTS(hc->base, caps->rtsoff);
- uintptr_t *erstba;
-
- /*
- * The spec states that the Event Ring Segment Table
- * Base Address register is at 'runtime_base + 0x30 +
- * (32 * interrupter)'. See the xHCI spec, section 5.5.2.3.2
- */
- erstba = XHCI_BASE_OFF(runtime_base, 0x30);
- *erstba = pa;
-}
-
-/*
- * Submit a command by pushing a TRB to the
- * command ring.
- *
- * @hc: Host controller descriptor.
- * @trb: Transfer Request Block of command.
- */
-static int
-xhci_submit_cmd(struct xhci_hc *hc, struct xhci_trb trb)
-{
- volatile uint32_t *cmd_db;
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
-
- /* Push the TRB to the command ring */
- hc->cmd_ring[hc->cmd_ptr++] = trb.dword0;
- hc->cmd_ring[hc->cmd_ptr++] = trb.dword1;
- hc->cmd_ring[hc->cmd_ptr++] = trb.dword2;
- hc->cmd_ring[hc->cmd_ptr++] = trb.dword3 | hc->cycle;
- hc->cmd_count++;
-
- /* Ring the command doorbell */
- cmd_db = XHCI_CMD_DB(hc->base, caps->dboff);
- *cmd_db = 0;
-
- if (hc->cmd_count >= XHCI_CMDRING_LEN - 1) {
- /*
- * Create raw link TRB and ring the doorbell. We want the
- * xHC to flip its cycle bit so it doesn't confuse existing
- * entries (that we'll overwrite) in the ring with current
- * entries, so we set the Toggle Cycle bit.
- *
- * See the xHCI spec, section 6.4.4.1 for information regarding
- * the format of link TRBs.
- */
- hc->cmd_ring[hc->cmd_ptr++] = VIRT_TO_PHYS(hc->cmd_ring) & 0xFFFFFFFF;
- hc->cmd_ring[hc->cmd_ptr++] = VIRT_TO_PHYS(hc->cmd_ring) >> 32;
- hc->cmd_ring[hc->cmd_ptr++] = 0;
- hc->cmd_ring[hc->cmd_ptr++] = hc->cycle | (XHCI_LINK << 10) | __BIT(1);
- *cmd_db = 0;
-
- /* Reset command state and flip cycle */
- hc->cmd_ptr = 0;
- hc->cmd_count = 0;
- hc->cycle = ~hc->cycle;
- }
-
- return 0;
-}
-
-/*
- * Parse xHCI extended caps
- */
-static int
-xhci_parse_ecp(struct xhci_hc *hc)
-{
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
- struct xhci_proto *proto;
- uint32_t *p, val, dword2;
- uint32_t cap_ptr = XHCI_ECP(caps->hccparams1);
-
- p = XHCI_BASE_OFF(hc->base, cap_ptr*4);
- while (cap_ptr != 0) {
- val = *p;
- dword2 = *((uint32_t *)XHCI_BASE_OFF(p, 8));
-
- /* Get the next cap */
- p = XHCI_BASE_OFF(p, XHCI_PROTO_NEXT(val) * 4);
- cap_ptr = XHCI_PROTO_NEXT(val);
-
- if (XHCI_PROTO_ID(val) != XHCI_ECAP_PROTO) {
- /* Not a Supported Protocol Capability */
- continue;
- }
-
- if (hc->protocnt > XHCI_MAX_PROTOS) {
- /* Too many protocols */
- break;
- }
-
- proto = &hc->protos[hc->protocnt++];
- proto->major = XHCI_PROTO_MAJOR(val);
- proto->port_count = XHCI_PROTO_PORTCNT(dword2);
- proto->port_start = XHCI_PROTO_PORTOFF(dword2);
- }
-
- return 0;
-}
-
-/*
- * Set of xHCI scratchpad buffers.
- */
-static int
-xhci_init_scratchpads(struct xhci_hc *hc)
-{
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
- uint16_t max_bufs_lo, max_bufs_hi, max_bufs;
- uintptr_t *buffer_array, tmp;
-
- max_bufs_lo = XHCI_MAX_SP_LO(caps->hcsparams2);
- max_bufs_hi = XHCI_MAX_SP_HI(caps->hcsparams2);
- max_bufs = (max_bufs_hi << 5) | max_bufs_lo;
- if (max_bufs == 0) {
- /*
- * Some emulators like QEMU don't need any
- * scratchpad buffers so we can just return
- * early.
- */
- return 0;
- }
-
- pr_trace("Need %d scratchpad buffers\n", max_bufs);
-
- /* Allocate buffer array */
- buffer_array = dynalloc_memalign(sizeof(uintptr_t)*max_bufs, 0x1000);
- if (buffer_array == NULL) {
- pr_error("Failed to allocate scratchpad buffer array\n");
- return -1;
- }
-
- memset(buffer_array, 0, sizeof(uintptr_t)*max_bufs);
-
- /* Fill the buffer array */
- for (size_t i = 0; i < max_bufs; ++i) {
- tmp = vm_alloc_pageframe(1);
-
- if (tmp == 0) {
- /* TODO: Shutdown, free memory */
- pr_error("Failed to fill scratchpad buffer array\n");
- return -1;
- }
-
- buffer_array[i] = tmp;
- }
-
- hc->dcbaap[0] = VIRT_TO_PHYS(buffer_array);
- return 0;
-}
-
-/*
- * Init USB ports on the root hub.
- */
-static int
-xhci_init_ports(struct xhci_hc *hc)
-{
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
- size_t maxports = XHCI_MAXPORTS(caps->hcsparams1);
- uint32_t *portsc;
-
- for (size_t i = 1; i < maxports; ++i) {
- portsc = xhci_get_portsc(hc, i);
- if (__TEST(*portsc, XHCI_PORTSC_CCS)) {
- pr_trace("Device connected on port %d, resetting...\n", i);
- *portsc |= XHCI_PORTSC_PR;
- }
- }
-
- return 0;
-}
-
-/*
- * Poll USBSTS.HCH to be val
- *
- * Returns 0 on success. Non-zero values returned
- * indicate a hang.
- */
-static int
-xhci_poll_hch(struct xhci_hc *hc, uint8_t val)
-{
- struct xhci_opregs *opregs = hc->opregs;
- size_t time_waiting = 0;
-
- while ((opregs->usbsts & USBSTS_HCH) != val) {
- if (time_waiting >= XHCI_TIMEOUT) {
- /* Hang */
- return -1;
- }
-
- time_waiting += 50;
- driver_tmr.msleep(50);
- }
-
- return 0;
-}
-
-/*
- * Start up the host controller by setting
- * the USBCMD run/stop bit.
- */
-static int
-xhci_start_hc(struct xhci_hc *hc)
-{
- struct xhci_opregs *opregs = hc->opregs;
- int status;
-
- opregs->usbcmd |= USBCMD_RUN;
-
- if ((status = xhci_poll_hch(hc, 0)) != 0) {
- return status;
- }
-
- return 0;
-}
-
-static int
-xhci_reset_hc(struct xhci_hc *hc)
-{
- struct xhci_opregs *opregs = hc->opregs;
- size_t time_waiting = 0; /* In ms */
-
- pr_trace("Resetting host controller...\n");
-
- /*
- * Set USBCMD.HCRST to reset the controller and
- * wait for it to become zero.
- */
- opregs->usbcmd |= USBCMD_HCRST;
- while (1) {
- if (!__TEST(opregs->usbcmd, USBCMD_HCRST)) {
- /* Reset is complete */
- break;
- }
- if (time_waiting >= XHCI_TIMEOUT) {
- pr_error("Hang while polling USBCMD.HCRST to be zero\n");
- return -1;
- }
- driver_tmr.msleep(50);
- time_waiting += 50;
- }
- return 0;
-}
-
-/*
- * Allocate the Device Context Base Address
- * Array.
- *
- * Returns the physical address and sets
- * hc->dcbaap to the virtual address.
- */
-static uintptr_t
-xhci_alloc_dcbaa(struct xhci_hc *hc)
-{
- size_t dcbaa_size;
-
- dcbaa_size = sizeof(uintptr_t) * hc->maxslots;
- hc->dcbaap = dynalloc_memalign(dcbaa_size, 0x1000);
- __assert(hc->dcbaap != NULL);
-
- return VIRT_TO_PHYS(hc->dcbaap);
-}
-
-/*
- * Allocates command ring and sets hc->cmd_ring
- * to the virtual address.
- *
- * Returns the physical address.
- */
-static uintptr_t
-xhci_alloc_cmdring(struct xhci_hc *hc)
-{
- size_t cmdring_size;
-
- cmdring_size = XHCI_TRB_SIZE * XHCI_CMDRING_LEN;
- hc->cmd_ring = dynalloc_memalign(cmdring_size, 0x1000);
- __assert(hc->cmd_ring != NULL);
-
- return VIRT_TO_PHYS(hc->cmd_ring);
-}
-
-/*
- * Sets up the event ring.
- */
-static void
-xhci_init_evring(struct xhci_hc *hc)
-{
- struct xhci_caps *caps = XHCI_CAPS(hc->base);
- struct xhci_evring_segment *seg;
- uint64_t *erdp, *erstba;
- uint32_t *erst_size;
- void *runtime = XHCI_RTS(hc->base, caps->rtsoff);
- size_t size;
-
- size = XHCI_EVRING_LEN * XHCI_TRB_SIZE;
- seg = dynalloc_memalign(size, 64);
- memset(seg, 0, size);
-
- /* Set the size of the event ring segment table */
- erst_size = XHCI_BASE_OFF(runtime, 0x28);
- *erst_size = 1;
-
- /* Setup the event ring segment */
- memset(seg, 0, size);
- seg->base = VIRT_TO_PHYS(seg);
- seg->size = XHCI_EVRING_LEN;
-
- /* Setup the event ring dequeue pointer */
- erdp = XHCI_BASE_OFF(runtime, 0x38);
- *erdp = seg->base;
-
- /* Point ERSTBA to our event ring segment */
- erstba = XHCI_BASE_OFF(runtime, 0x30);
- *erstba = VIRT_TO_PHYS(seg);
-
- hc->event_ring = PHYS_TO_VIRT(seg->base);
-}
-
-static int
-xhci_init_hc(struct xhci_hc *hc)
-{
- struct xhci_caps *caps;
- struct xhci_opregs *opregs;
-
- /* Get some information from the controller */
- caps = XHCI_CAPS(hc->base);
- hc->caplen = caps->caplength;
- hc->maxslots = XHCI_MAXSLOTS(caps->hcsparams1);
- hc->maxports = XHCI_MAXSLOTS(caps->hcsparams1);
-
- opregs->config |= hc->maxslots;
-
- /* Fetch the opregs */
- opregs = XHCI_OPBASE(hc->base, hc->caplen);
- hc->opregs = XHCI_OPBASE(hc->base, hc->caplen);
-
- if (xhci_reset_hc(hc) != 0) {
- return -1;
- }
-
- /* Set cmdring state */
- hc->cycle = 1;
- hc->cmd_ptr = 0;
- hc->cmd_count = 0;
-
- /* Allocate resources and tell the HC about them */
- opregs->dcbaa_ptr = xhci_alloc_dcbaa(hc);
- xhci_init_scratchpads(hc);
- xhci_init_evring(hc);
- opregs->cmd_ring = xhci_alloc_cmdring(hc);
-
- /* We're ready, start up the HC and ports */
- xhci_start_hc(hc);
- xhci_parse_ecp(hc);
- xhci_init_ports(hc);
- return 0;
-}
-
-static int
-xhci_init(void)
-{
- uintptr_t bar0, bar1, base;
- struct xhci_hc hc;
- struct pci_lookup hc_lookup = {
- .pci_class = 0x0C,
- .pci_subclass = 0x03
- };
-
- /* Find the host controller on the bus */
- hci_dev = pci_get_device(hc_lookup, PCI_CLASS | PCI_SUBCLASS);
- if (hci_dev == NULL) {
- return -1;
- }
-
- bar0 = hci_dev->bar[0] & ~7;
- bar1 = hci_dev->bar[1] & ~7;
- base = __COMBINE32(bar1, bar0);
- hc.base = PHYS_TO_VIRT(base);
- pr_trace("xHCI HC base @ 0x%p\n", base);
-
- if (req_timer(TIMER_GP, &driver_tmr) != 0) {
- pr_error("Failed to fetch general purpose timer\n");
- return -1;
- }
- if (driver_tmr.msleep == NULL) {
- pr_error("Timer does not have msleep()\n");
- return -1;
- }
-
- return xhci_init_hc(&hc);
-}
-
-DRIVER_EXPORT(xhci_init);
diff --git a/sys/dev/vcons/vcons.c b/sys/dev/vcons/vcons.c
deleted file mode 100644
index fe1a3f0..0000000
--- a/sys/dev/vcons/vcons.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <dev/vcons/vcons.h>
-#include <dev/vcons/vcons_io.h>
-#include <dev/video/fbdev.h>
-#include <sys/syslog.h>
-#include <sys/cdefs.h>
-#include <string.h>
-
-__MODULE_NAME("kern_vcons");
-__KERNEL_META("$Hyra$: kern_vcons.c, Ian Marco Moffett, "
- "Hyra video console code");
-
-/* Get x or y values in pixels */
-#define PIX_CPY_X(scrptr) ((scrptr)->cpy_x * FONT_WIDTH)
-#define PIX_CPY_Y(scrptr) ((scrptr)->cpy_y * FONT_HEIGHT)
-
-#define PIX_BOUNDS_MAX_X(scrptr) ((scrptr)->fbdev.width - FONT_WIDTH)
-#define PIX_BOUNDS_MAX_Y(scrptr) ((scrptr)->fbdev.height - FONT_HEIGHT)
-
-static struct vcons_screen *screen = NULL;
-
-/*
- * Draw the console cursor.
- *
- * @color: Cursor color
- */
-static void
-vcons_draw_cursor(struct vcons_screen *scr, uint32_t color)
-{
- struct vcons_cursor *cursor = &scr->cursor;
- struct fbdev fbdev = scr->fbdev;
-
- uint32_t *fbdev_mem = fbdev.mem;
- uint32_t fbdev_idx = 0;
- uint32_t cx, cy;
-
- for (size_t y = VCONS_CURSOR_HEIGHT; y > 0; --y) {
- for (size_t x = VCONS_CURSOR_WIDTH; x > 0; --x) {
- cx = cursor->old_xpos + x;
- cy = cursor->old_ypos + y;
-
- fbdev_idx = fbdev_get_index(&fbdev, cx, cy);
- fbdev_mem[fbdev_idx] = color;
- }
- }
-}
-
-/*
- * Clear everything out of the console.
- */
-static void
-vcons_clear_scr(struct vcons_screen *scr)
-{
- struct fbdev fbdev = scr->fbdev;
-
- scr->cpy_x = 0, scr->cpy_y = 0;
-
- memset(scr->fbdev_mem, scr->bg, (fbdev.pitch * fbdev.height));
- vcons_update_cursor(scr);
-}
-
-/*
- * Renders a char onto the screen specified by `scr`.
- *
- * @x,y: In chars
- */
-static void
-vcons_draw_char(struct vcons_screen *scr, char c, uint32_t x, uint32_t y)
-{
- uint32_t *fb_ptr;
- size_t idx;
- const uint8_t *glyph;
-
- /* Get a pointer to framebuffer memory */
- fb_ptr = scr->fbdev_mem;
-
- /* Get the specific glyph of `c` */
- glyph = &DEFAULT_FONT_DATA[(int)c*16];
-
- for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) {
- for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) {
- idx = fbdev_get_index(&scr->fbdev, x+FONT_WIDTH-cx, y+cy);
- fb_ptr[idx] = __TEST(glyph[cy], __BIT(cx)) ? scr->fg : scr->bg;
- }
- }
-}
-
-/*
- * Update the cursor position.
- *
- * XXX: This function also accounts for the old cursor
- * and clears it before drawing the new cursor.
- */
-void
-vcons_update_cursor(struct vcons_screen *scr)
-{
- struct vcons_cursor *cursor = &scr->cursor;
-
- cursor->is_drawing = true;
-
- if (cursor->is_drawn) {
- /* Clear old cursor */
- vcons_draw_cursor(scr, scr->bg);
- }
-
- cursor->old_xpos = cursor->xpos;
- cursor->old_ypos = cursor->ypos;
- vcons_draw_cursor(scr, scr->fg);
-
- cursor->is_drawn = true;
- cursor->is_drawing = false;
-}
-
-/*
- * Write out a character on the console.
- *
- * @c: Character to write.
- */
-int
-vcons_putch(struct vcons_screen *scr, char c)
-{
- uint32_t x = PIX_CPY_X(scr);
- uint32_t y = PIX_CPY_Y(scr);
- struct vcons_cursor *cursor = &scr->cursor;
- bool cursor_newline = false;
-
- while (cursor->is_drawing);
-
- if (scr == NULL) {
- return 1;
- }
-
- /* Check cursor bounds */
- if (cursor->xpos >= PIX_BOUNDS_MAX_X(scr)) {
- cursor->xpos = FONT_WIDTH;
- cursor->ypos += FONT_HEIGHT;
- cursor_newline = true;
- }
- if (cursor->ypos >= PIX_BOUNDS_MAX_Y(scr)) {
- cursor->xpos = FONT_WIDTH;
- cursor->ypos = 0;
- }
-
- /* Check text bounds */
- if (x >= PIX_BOUNDS_MAX_X(scr)) {
- /* Wrap to the next row */
- ++scr->cpy_y, scr->cpy_x = 0;
- x = PIX_CPY_X(scr), y = PIX_CPY_Y(scr);
- }
- if (y >= PIX_BOUNDS_MAX_Y(scr)) {
- scr->cpy_y = 0;
- scr->cpy_x = 0;
- vcons_clear_scr(scr);
- x = PIX_CPY_X(scr), y = PIX_CPY_Y(scr);
- }
-
- if (!cursor_newline) {
- cursor->xpos += FONT_WIDTH;
- }
-
- vcons_update_cursor(scr);
- vcons_draw_char(scr, c, x, y);
- ++scr->cpy_x;
- return 0;
-}
-
-/*
- * Write out a string on the console.
- *
- * @s: String to write.
- */
-int
-vcons_putstr(struct vcons_screen *scr, const char *s, size_t len)
-{
- int status;
-
- for (size_t i = 0; i < len; ++i) {
- if (vcons_process_output(scr, s[i]) > 0)
- continue;
- if ((status = vcons_putch(scr, s[i])) != 0) {
- return status;
- }
- }
-
- return 0;
-}
-
-void
-vcons_attach(struct vcons_screen *scr)
-{
- scr->fbdev = fbdev_get_front();
- scr->fbdev_mem = scr->fbdev.mem;
-
- scr->nrows = scr->fbdev.height;
- scr->ncols = scr->fbdev.width;
-
- screen = scr;
- vcons_clear_scr(scr);
-}
diff --git a/sys/dev/vcons/vcons_io.c b/sys/dev/vcons/vcons_io.c
deleted file mode 100644
index f4e693e..0000000
--- a/sys/dev/vcons/vcons_io.c
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <dev/vcons/vcons_io.h>
-#include <dev/vcons/vcons.h>
-#include <sys/cdefs.h>
-#include <sys/ascii.h>
-
-static void
-vcons_expand_tab(struct vcons_screen *scr)
-{
- for (size_t i = 0; i < VCONS_TAB_WIDTH; ++i) {
- vcons_putch(scr, ' ');
- }
-}
-
-/*
- * This routine tries to process the output `c'.
- *
- * Returns < 0 value on failure. Values >= 0
- * is `c' which may differ from the original.
- *
- * This routine also may modify the screen state
- * if `c' is a control character.
- */
-int
-vcons_process_output(struct vcons_screen *scr, int c)
-{
- struct vcons_cursor *cursor = &scr->cursor;
-
- switch (c) {
- case ASCII_LF:
- scr->cpy_y++;
- cursor->ypos += FONT_HEIGHT;
-
- scr->cpy_x = 0;
- cursor->xpos = 0;
- break;
- case ASCII_CR:
- scr->cpy_x = 0;
- cursor->xpos = 0;
- break;
- case ASCII_HT:
- vcons_expand_tab(scr);
- break;
- case ASCII_BS:
- if (cursor->xpos > 0) {
- scr->cpy_x--;
- cursor->xpos -= FONT_WIDTH;
- }
- break;
- default:
- return -1;
- }
-
- vcons_update_cursor(scr);
- return c;
-}
diff --git a/sys/dev/video/fbdev.c b/sys/dev/video/fbdev.c
deleted file mode 100644
index d9b680f..0000000
--- a/sys/dev/video/fbdev.c
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Hyra nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-#include <sys/limine.h>
-#include <sys/device.h>
-#include <sys/driver.h>
-#include <sys/system.h>
-#include <sys/errno.h>
-#include <dev/video/fbdev.h>
-#include <vm/vm.h>
-#include <fs/devfs.h>
-
-#define FRAMEBUFFER \
- framebuffer_req.response->framebuffers[0]
-
-static volatile struct limine_framebuffer_request framebuffer_req = {
- .id = LIMINE_FRAMEBUFFER_REQUEST,
- .revision = 0
-};
-
-static struct device *dev;
-
-static int
-fbdev_ioctl(struct device *dev, uint32_t cmd, uintptr_t arg)
-{
- struct fbdev_info info = {
- .width = FRAMEBUFFER->width,
- .height = FRAMEBUFFER->height,
- .pitch = FRAMEBUFFER->pitch,
- .bits_per_pixel = FRAMEBUFFER->bpp
- };
-
- switch (cmd) {
- case FBIOCTL_INFO:
- copyout(&info, arg, sizeof(info));
- break;
- default:
- return -EINVAL;
- }
-
- return 0;
-}
-
-static paddr_t
-fbdev_mmap(struct device *dev, off_t off, vm_prot_t prot)
-{
- struct fbdev fbdev = fbdev_get_front();
- paddr_t len = fbdev.width * fbdev.pitch;
- paddr_t base = VIRT_TO_PHYS(fbdev.mem);
- paddr_t max_paddr = base + len;
-
- if (base + off > max_paddr) {
- return 0;
- }
-
- return base + off;
-}
-
-static int
-fbdev_open(struct device *dev)
-{
- return 0;
-}
-
-struct fbdev
-fbdev_get_front(void)
-{
- struct fbdev ret;
-
- ret.mem = FRAMEBUFFER->address;
- ret.width = FRAMEBUFFER->width;
- ret.height = FRAMEBUFFER->height;
- ret.pitch = FRAMEBUFFER->pitch;
- return ret;
-}
-
-static int
-fbdev_init(void)
-{
- dev = device_alloc();
- dev->blocksize = 1;
- dev->read = NULL;
- dev->write = NULL;
- dev->mmap = fbdev_mmap;
- dev->ioctl = fbdev_ioctl;
- dev->open = fbdev_open;
-
- device_create(dev, device_alloc_major(), 1);
- devfs_add_dev("fb", dev);
- return 0;
-}
-
-DRIVER_EXPORT(fbdev_init);