[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cdi-devel] [PATCH 3/4] cdi: Replace cdi_alloc_phys_mem/addr



* In all drivers, replace cdi_alloc_phys_mem by cdi_mem_alloc
* In all drivers, replace cdi_alloc_phys_addr by cdi_mem_map

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 ata/device.c              |   16 +++++++++---
 ata/device.h              |    4 +-
 ata/request.c             |    2 +-
 e1000/device.c            |   19 +++++++++----
 e1000/device.h            |    2 +-
 ne2k/include/ne2k.h       |    4 +-
 ne2k/ne2k.c               |   13 +++++++--
 pcnet/pcnet.c             |   61 +++++++++++++++++++++++++++-----------------
 rtl8139/include/rtl8139.h |    4 +-
 rtl8139/rtl8139.c         |   13 +++++++--
 sis900/device.c           |   15 ++++++++---
 sis900/device.h           |    2 +-
 12 files changed, 102 insertions(+), 53 deletions(-)

diff --git a/ata/device.c b/ata/device.c
index dd087c3..6e33d52 100644
--- a/ata/device.c
+++ b/ata/device.c
@@ -34,6 +34,7 @@
 #include "cdi/misc.h"
 #include "cdi/io.h"
 #include "cdi/scsi.h"
+#include "cdi/mem.h"
 
 #include "device.h"
 
@@ -265,10 +266,17 @@ void ata_init_controller(struct ata_controller* controller)
 
     // Abschliessend wird noch DMA vorbereitet, wenn moeglich
     if (controller->port_bmr_base) {
-        cdi_alloc_phys_mem(sizeof(uint64_t), (void**) &controller->prdt_virt,
-            (void**) &controller->prdt_phys);
-        cdi_alloc_phys_mem(ATA_DMA_MAXSIZE, (void**) &controller->dma_buf_virt,
-            (void**) &controller->dma_buf_phys);
+        struct cdi_mem_area* buf;
+
+        buf = cdi_mem_alloc(sizeof(uint64_t),
+            CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G);
+        controller->prdt_virt = buf->vaddr;
+        controller->prdt_phys = buf->paddr.items[0].start;
+
+        buf = cdi_mem_alloc(ATA_DMA_MAXSIZE,
+            CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G);
+        controller->dma_buf_virt = buf->vaddr;
+        controller->dma_buf_phys = buf->paddr.items[0].start;
 
         controller->dma_use = 1;
     }
diff --git a/ata/device.h b/ata/device.h
index efb3f6f..6c438e0 100644
--- a/ata/device.h
+++ b/ata/device.h
@@ -367,11 +367,11 @@ struct ata_controller {
 
 
     /// Physische Adresse der Physical Region Descriptor Table (fuer DMA)
-    uint64_t*                   prdt_phys;
+    uintptr_t                   prdt_phys;
     /// Virtuelle Adresse der Physical Region Descriptor Table (fuer DMA)
     uint64_t*                   prdt_virt;
     /// Physische Adresse des DMA-Puffers
-    void*                       dma_buf_phys;
+    uintptr_t                   dma_buf_phys;
     /// Virtuelle Adresse des DMA-Puffers
     void*                       dma_buf_virt;
 };
diff --git a/ata/request.c b/ata/request.c
index e98ee60..9d618c7 100644
--- a/ata/request.c
+++ b/ata/request.c
@@ -433,7 +433,7 @@ static int ata_request_dma_init(struct ata_request* request)
         BMR_STATUS_IRQ);
 
     // Adresse der PRDT eintragen
-    cdi_outl(ctrl->port_bmr_base + BMR_PRDT, (uint32_t) ctrl->prdt_phys);
+    cdi_outl(ctrl->port_bmr_base + BMR_PRDT, ctrl->prdt_phys);
 
     if (request->flags.direction != READ) {
         memcpy(ctrl->dma_buf_virt, request->buffer, size);
diff --git a/e1000/device.c b/e1000/device.c
index b21df2f..1b31987 100644
--- a/e1000/device.c
+++ b/e1000/device.c
@@ -35,6 +35,7 @@
 #include "cdi.h"
 #include "cdi/misc.h"
 #include "cdi/pci.h"
+#include "cdi/mem.h"
 
 #include "device.h"
 #include "e1000_io.h"
@@ -42,7 +43,7 @@
 #undef DEBUG
 
 #define PHYS(netcard, field) \
-    ((uintptr_t) netcard->phys + offsetof(struct e1000_device, field))
+    (netcard->phys + offsetof(struct e1000_device, field))
 
 static void e1000_handle_interrupt(struct cdi_device* device);
 static uint64_t get_mac_address(struct e1000_device* device);
@@ -150,16 +151,22 @@ struct cdi_device* e1000_init_device(struct cdi_bus_data* bus_data)
 {
     struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data;
     struct e1000_device* netcard;
-    void* phys_device;
+    struct cdi_mem_area* buf;
 
     if (!((pci->vendor_id == 0x8086) && (pci->device_id == 0x100e))) {
         return NULL;
     }
 
-    cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device);
+    buf = cdi_mem_alloc(sizeof(*netcard),
+        CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G | 2);
+    if (buf == NULL) {
+        return NULL;
+    }
+
+    netcard = buf->vaddr;
     memset(netcard, 0, sizeof(*netcard));
 
-    netcard->phys = phys_device;
+    netcard->phys = buf->paddr.items[0].start;
     netcard->net.dev.bus_data = (struct cdi_bus_data*) pci;
 
     // PCI-bezogenes Zeug initialisieren
@@ -172,8 +179,8 @@ struct cdi_device* e1000_init_device(struct cdi_bus_data* bus_data)
     int i;
     for (i = 0; (res = cdi_list_get(reslist, i)); i++) {
         if (res->type == CDI_PCI_MEMORY) {
-            netcard->mem_base =
-                cdi_alloc_phys_addr(res->length, res->start);
+            struct cdi_mem_area* mmio = cdi_mem_map(res->start, res->length);
+            netcard->mem_base = mmio->vaddr;
         }
     }
 
diff --git a/e1000/device.h b/e1000/device.h
index 9266e0f..2a10365 100644
--- a/e1000/device.h
+++ b/e1000/device.h
@@ -143,7 +143,7 @@ struct e1000_rx_descriptor {
 struct e1000_device {
     struct cdi_net_device       net;
 
-    void*                       phys;
+    uintptr_t                   phys;
 
     struct e1000_tx_descriptor  tx_desc[TX_BUFFER_NUM];
     uint8_t                     tx_buffer[TX_BUFFER_NUM * TX_BUFFER_SIZE];
diff --git a/ne2k/include/ne2k.h b/ne2k/include/ne2k.h
index 585eab5..0102ce8 100644
--- a/ne2k/include/ne2k.h
+++ b/ne2k/include/ne2k.h
@@ -71,7 +71,7 @@
 #define NE_DATA     0x10
 
 #define PHYS(netcard, field) \
-    ((uintptr_t) netcard->phys + offsetof(struct rtl8139_device, field))
+    (netcard->phys + offsetof(struct rtl8139_device, field))
 
 #define RX_BUFFER_SIZE 0x2000
 #define TX_BUFFER_SIZE 0x1000
@@ -84,7 +84,7 @@ typedef struct {
 struct ne2k_device {
     struct cdi_net_device       net;
 
-    void*                       phys;
+    uintptr_t                   phys;
     uint16_t                    port_base;
 
     uint8_t                     next_packet;
diff --git a/ne2k/ne2k.c b/ne2k/ne2k.c
index 7f1262e..d3fa019 100644
--- a/ne2k/ne2k.c
+++ b/ne2k/ne2k.c
@@ -35,6 +35,7 @@
 #include "cdi/pci.h"
 #include "cdi/io.h"
 #include "cdi/misc.h"
+#include "cdi/mem.h"
 
 #include "ne2k.h"
 #include "ethernet.h"
@@ -81,16 +82,22 @@ struct cdi_device* ne2k_init_device(struct cdi_bus_data* bus_data)
 {
     struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data;
     struct ne2k_device* netcard;
-    void* phys_device;
+    struct cdi_mem_area* buf;
 
     if (!((pci->vendor_id == 0x10ec) && (pci->device_id == 0x8029))) {
         return NULL;
     }
 
-    cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device);
+    buf = cdi_mem_alloc(sizeof(*netcard),
+        CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G | 2);
+    if (buf == NULL) {
+        return NULL;
+    }
+
+    netcard = buf->vaddr;
     memset(netcard, 0, sizeof(*netcard));
 
-    netcard->phys = phys_device;
+    netcard->phys = buf->paddr.items[0].start;
     netcard->net.dev.bus_data = (struct cdi_bus_data*) pci;
 
     // PCI-bezogenes Zeug initialisieren
diff --git a/pcnet/pcnet.c b/pcnet/pcnet.c
index e4500dc..ea0b4d7 100644
--- a/pcnet/pcnet.c
+++ b/pcnet/pcnet.c
@@ -28,6 +28,7 @@
 
 #include "pcnet.h"
 #include "cdi/io.h"
+#include "cdi/mem.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -242,14 +243,14 @@ static void pcnet_send_init_block(struct pcnet_device* netcard, int promisc)
         (struct cdi_pci_device*) netcard->net.dev.bus_data;
 
     struct initialization_block* initialization_block;
-    void* phys_initialization_block;
-
+    struct cdi_mem_area* buf;
 
     // Fill the initialization block
     // NOTE: Transmit and receive buffer contain 8 entries
-    cdi_alloc_phys_mem(sizeof(struct initialization_block),
-                       (void**) &initialization_block,
-                       &phys_initialization_block);
+    buf = cdi_mem_alloc(sizeof(struct initialization_block),
+        CDI_MEM_DMA_4G | CDI_MEM_PHYS_CONTIGUOUS);
+
+    initialization_block = buf->vaddr;
     memset(initialization_block, 0, sizeof(struct initialization_block));
     initialization_block->mode = promisc ? PROMISCUOUS_MODE : 0;
     initialization_block->receive_length = 0x30;
@@ -263,12 +264,12 @@ static void pcnet_send_init_block(struct pcnet_device* netcard, int promisc)
     #ifdef DEBUG
         printf("pcnet: initialization block at 0x%p virtual, 0x%p physical\n",
                initialization_block,
-               phys_initialization_block);
+               (void*) buf->paddr.items[0].start);
     #endif
 
     // Set the address for the initialization block
-    pcnet_write_csr(netcard, 1, (uintptr_t) phys_initialization_block);
-    pcnet_write_csr(netcard, 2, (uintptr_t) phys_initialization_block >> 16);
+    pcnet_write_csr(netcard, 1, buf->paddr.items[0].start);
+    pcnet_write_csr(netcard, 2, buf->paddr.items[0].start >> 16);
 
     // TODO BCR18.BREADE / BWRITE
 
@@ -282,41 +283,50 @@ static void pcnet_send_init_block(struct pcnet_device* netcard, int promisc)
     if(irq < 0 || netcard->init_wait_for_irq == 1)
         printf("pcnet: waiting for IRQ failed: %d\n", irq);
 
-    // FIXME Free initialization block
+    // Free initialization block
+    cdi_mem_free(buf);
 }
 
 void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous)
 {
     // Allocate the receive & transmit descriptor buffer
-    void *virt_desc_region;
-    void *phys_desc_region;
-    if (cdi_alloc_phys_mem(4096, &virt_desc_region, &phys_desc_region) == -1)
-    {
+    struct cdi_mem_area* desc;
+
+    desc = cdi_mem_alloc(4096, CDI_MEM_DMA_4G | CDI_MEM_PHYS_CONTIGUOUS);
+    if (desc == NULL) {
         printf("pcnet: failed to allocate descriptor region\n");
         return;
     }
 
     #ifdef DEBUG
-        printf("pcnet: descriptor region at 0x%p virtual and 0x%p physical\n", virt_desc_region, phys_desc_region);
+        printf("pcnet: descriptor region at 0x%p virtual and 0x%p physical\n",
+            desc->vaddr, (void*) desc->paddr.items[0].start);
     #endif
-    netcard->receive_descriptor = virt_desc_region;
-    netcard->transmit_descriptor = (struct transmit_descriptor*) (((char*)virt_desc_region) + 2 * 1024);
-    netcard->phys_receive_descriptor = phys_desc_region;
-    netcard->phys_transmit_descriptor = (void*) (((char*)phys_desc_region) + 2 * 1024);
+    netcard->receive_descriptor = desc->vaddr;
+    netcard->transmit_descriptor = (struct transmit_descriptor*)
+        ((char*) desc->vaddr + 2 * 1024);
+    netcard->phys_receive_descriptor = (void*) desc->paddr.items[0].start;
+    netcard->phys_transmit_descriptor = (void*)
+        ((desc->paddr.items[0].start) + 2 * 1024);
 
     
     // Allocate the buffers
     memset(netcard->transmit_descriptor, 0, 2048);
-    void *virt_buffer;
-    void *phys_buffer;
     int i = 0;
     for (;i < 4;i++)
     {
-        if (cdi_alloc_phys_mem(4096, &virt_buffer, &phys_buffer) == -1)
-        {
+        void* virt_buffer;
+        uintptr_t phys_buffer;
+        struct cdi_mem_area* buf;
+
+        buf = cdi_mem_alloc(4096, CDI_MEM_DMA_4G | CDI_MEM_PHYS_CONTIGUOUS);
+        if (buf == 0) {
             DEBUG_MSG("cdi_alloc_phys_mem failed");
             return;
         }
+        virt_buffer = buf->vaddr;
+        phys_buffer = buf->paddr.items[0].start;
+
         netcard->receive_buffer[2 * i] = virt_buffer;
         netcard->receive_buffer[2 * i + 1] = ((char*)virt_buffer) + 2048;
         netcard->receive_descriptor[2 * i].address = (uint32_t) phys_buffer;
@@ -328,11 +338,14 @@ void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous)
         netcard->receive_descriptor[2 * i + 1].flags2 = 0;
         netcard->receive_descriptor[2 * i + 1].res = 0;
 
-        if (cdi_alloc_phys_mem(4096, &virt_buffer, &phys_buffer) == -1)
-        {
+        buf = cdi_mem_alloc(4096, CDI_MEM_DMA_4G | CDI_MEM_PHYS_CONTIGUOUS);
+        if (buf == 0) {
             DEBUG_MSG("cdi_alloc_phys_mem failed");
             return;
         }
+        virt_buffer = buf->vaddr;
+        phys_buffer = buf->paddr.items[0].start;
+
         netcard->transmit_buffer[2 * i] = virt_buffer;
         netcard->transmit_buffer[2 * i + 1] = ((char*)virt_buffer) + 2048;
         netcard->transmit_descriptor[2 * i].address = (uint32_t) phys_buffer;
diff --git a/rtl8139/include/rtl8139.h b/rtl8139/include/rtl8139.h
index 3ec81b0..aafa71f 100644
--- a/rtl8139/include/rtl8139.h
+++ b/rtl8139/include/rtl8139.h
@@ -70,7 +70,7 @@
 #define ISR_RECEIVE_OK                  (1 << 0)
 
 #define PHYS(netcard, field) \
-    ((uintptr_t) netcard->phys + offsetof(struct rtl8139_device, field))
+    (netcard->phys + offsetof(struct rtl8139_device, field))
 
 #define RX_BUFFER_SIZE 0x2000
 #define TX_BUFFER_SIZE 0x1000
@@ -83,7 +83,7 @@ typedef struct {
 struct rtl8139_device {
     struct cdi_net_device       net;
 
-    void*                       phys;
+    uintptr_t                   phys;
     uint16_t                    port_base;
 
     uint8_t                     buffer[TX_BUFFER_SIZE]
diff --git a/rtl8139/rtl8139.c b/rtl8139/rtl8139.c
index 600f3e9..6571c56 100644
--- a/rtl8139/rtl8139.c
+++ b/rtl8139/rtl8139.c
@@ -35,6 +35,7 @@
 #include "cdi/pci.h"
 #include "cdi/io.h"
 #include "cdi/misc.h"
+#include "cdi/mem.h"
 
 #include "rtl8139.h"
 #include "ethernet.h"
@@ -79,16 +80,22 @@ struct cdi_device* rtl8139_init_device(struct cdi_bus_data* bus_data)
 {
     struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data;
     struct rtl8139_device* netcard;
-    void* phys_device;
+    struct cdi_mem_area* buf;
 
     if (!((pci->vendor_id == 0x10ec) && (pci->device_id == 0x8139))) {
         return NULL;
     }
 
-    cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device);
+    buf = cdi_mem_alloc(sizeof(*netcard),
+        CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G | 2);
+    if (buf == NULL) {
+        return NULL;
+    }
+
+    netcard = buf->vaddr;
     memset(netcard, 0, sizeof(*netcard));
 
-    netcard->phys = phys_device;
+    netcard->phys = buf->paddr.items[0].start;
     netcard->net.dev.bus_data = (struct cdi_bus_data*) pci;
 
     // PCI-bezogenes Zeug initialisieren
diff --git a/sis900/device.c b/sis900/device.c
index 30c6d39..aa5d7c8 100644
--- a/sis900/device.c
+++ b/sis900/device.c
@@ -34,6 +34,7 @@
 
 #include "cdi.h"
 #include "cdi/misc.h"
+#include "cdi/mem.h"
 
 #include "device.h"
 #include "sis900_io.h"
@@ -46,7 +47,7 @@
 #undef DEBUG
 
 #define PHYS(netcard, field) \
-    ((uintptr_t) netcard->phys + offsetof(struct sis900_device, field))
+    (netcard->phys + offsetof(struct sis900_device, field))
 
 static void sis900_handle_interrupt(struct cdi_device* device);
 
@@ -185,16 +186,22 @@ struct cdi_device* sis900_init_device(struct cdi_bus_data* bus_data)
 {
     struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data;
     struct sis900_device* netcard;
-    void* phys_device;
+    struct cdi_mem_area* buf;
 
     if (!((pci->vendor_id == 0x1039) && (pci->device_id == 0x0900))) {
         return NULL;
     }
 
-    cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device);
+    buf = cdi_mem_alloc(sizeof(*netcard),
+        CDI_MEM_PHYS_CONTIGUOUS | CDI_MEM_DMA_4G | 2);
+    if (buf == NULL) {
+        return NULL;
+    }
+
+    netcard = buf->vaddr;
     memset(netcard, 0, sizeof(*netcard));
 
-    netcard->phys = phys_device;
+    netcard->phys = buf->paddr.items[0].start;
     netcard->net.dev.bus_data = (struct cdi_bus_data*) pci;
 
     // PCI-bezogenes Zeug initialisieren
diff --git a/sis900/device.h b/sis900/device.h
index cd110c0..7698d2f 100644
--- a/sis900/device.h
+++ b/sis900/device.h
@@ -129,7 +129,7 @@ struct sis900_tx_descriptor {
 struct sis900_device {
     struct cdi_net_device       net;
 
-    void*                       phys;
+    uintptr_t                   phys;
 
     struct sis900_tx_descriptor tx_desc[TX_BUFFER_NUM];
     uint8_t                     tx_buffer[TX_BUFFER_NUM * TX_BUFFER_SIZE];
-- 
1.6.0.2