cma_api_impl.h 4.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/*
 * The MIT License (MIT)
 *
 * COPYRIGHT (C) 2017 Institute of Electronics and Computer Science (EDI), Latvia.
 * AUTHOR: Rihards Novickis (rihards.novickis@edi.lv)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

/*!
 *  Copyright (c) 2018 by Contributors
 * \file cma_api.cc
 * \brief Application layer implementation for contigous memory allocation.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "cma_api.h"

#ifndef CMA_IOCTL_MAGIC
#define CMA_IOCTL_MAGIC       0xf2
#endif

#define CMA_ALLOC_CACHED      _IOC(_IOC_WRITE|_IOC_READ,  CMA_IOCTL_MAGIC, 1, 4)
#define CMA_ALLOC_NONCACHED   _IOC(_IOC_WRITE|_IOC_READ,  CMA_IOCTL_MAGIC, 2, 4)
#define CMA_FREE              _IOC(_IOC_WRITE,            CMA_IOCTL_MAGIC, 3, 4)
#define CMA_GET_PHY_ADDR      _IOC(_IOC_WRITE|_IOC_READ,  CMA_IOCTL_MAGIC, 4, 4)
#define CMA_GET_SIZE          _IOC(_IOC_WRITE|_IOC_READ,  CMA_IOCTL_MAGIC, 5, 4)

#define CMA_IOCTL_MAXNR       5

#ifndef CMA_DEBUG
  #define CMA_DEBUG           0
#endif
#ifndef DRIVER_NODE_NAME
  #define DRIVER_NODE_NAME    "cma"
#endif

#if CMA_DEBUG == 1
  #define __DEBUG(fmt, args...)  printf("CMA_API_DEBUG: " fmt, ##args)
#else
  #define __DEBUG(fmt, args...)
#endif

#define ROUND_UP(N, S)     ((((N) + (S) - 1) / (S)) * (S))


/* Private functions */
void *cma_alloc(size_t size, unsigned ioctl_cmd);

/* Global file descriptor */
int cma_fd = 0;

int cma_init(void) {
  __DEBUG("Opening \"/dev/" DRIVER_NODE_NAME "\" file\n");

  cma_fd = open("/dev/" DRIVER_NODE_NAME, O_RDWR);
  if (cma_fd == -1) {
    __DEBUG("Failed to initialize api - \"%s\"\n", strerror(errno));
    return -1;
  }

  return 0;
}

int cma_release(void) {
  __DEBUG("Closing \"/dev/" DRIVER_NODE_NAME "\" file\n");

  if (close(cma_fd) == -1) {
    __DEBUG("Failed to finilize api - \"%s\"\n", strerror(errno));
    return -1;
  }

  return 0;
}

void *cma_alloc_cached(size_t size) {
  return cma_alloc(size, CMA_ALLOC_CACHED);
}

void *cma_alloc_noncached(size_t size) {
  return cma_alloc(size, CMA_ALLOC_NONCACHED);
}

int cma_free(void *mem) {
  __DEBUG("Releasing contigous memory from 0x%x\n", (unsigned)mem);
  unsigned data, v_addr;

  /* save user space pointer value */
  data   = (unsigned)mem;
  v_addr = (unsigned)mem;

  if ( ioctl(cma_fd, CMA_GET_SIZE, &data) == -1 ) {
    __DEBUG("cma_free - ioctl command unsuccsessful - 0\n");
    return -1;
  }
  /* data now contains size */

  /* unmap memory */
  munmap(mem, data);

  /* free cma entry */
  if ( ioctl(cma_fd, CMA_FREE, &v_addr) == -1 ) {
    __DEBUG("cma_free - ioctl command unsuccsessful - 1\n");
    return -1;
  }

  return 0;
}

unsigned cma_get_phy_addr(void *mem) {
  unsigned data;
  __DEBUG("Getting physical address from 0x%x\n", (unsigned)mem);

  /* save user space pointer value */
  data = (unsigned)mem;

  /* get physical address */
  if ( ioctl(cma_fd, CMA_GET_PHY_ADDR, &data) == -1 ) {
    __DEBUG("cma_free - ioctl command unsuccsessful\n");
    return 0;
  }
  /* data now contains physical address */

  return data;
}


void *cma_alloc(size_t size, unsigned ioctl_cmd) {
  unsigned data;
  void   *mem;
  __DEBUG("Allocating 0x%x bytes of contigous memory\n", size);

  /* Page align size */
  size = ROUND_UP(size, getpagesize());

  /* ioctl cmd to allocate contigous memory */
  data = (unsigned)size;
  if ( ioctl(cma_fd, ioctl_cmd, &data) == -1 ) {
    __DEBUG("cma_alloc - ioctl command unsuccsessful\n");
    return NULL;
  }

  /* at this point phy_addr is written to data */

  /* mmap memory */
  mem = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, cma_fd, data);
  if (mem == MAP_FAILED) {
    __DEBUG("cma_alloc - mmap unsuccsessful\n");
    return NULL;
  }

  return mem;
}