| 1 | /* |
|---|
| 2 | * kern/sys_dma_memcpy.c - exported DMA access to user process |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
|---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
|---|
| 6 | * |
|---|
| 7 | * This file is part of ALMOS-kernel. |
|---|
| 8 | * |
|---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
|---|
| 10 | * under the terms of the GNU General Public License as published by |
|---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 12 | * |
|---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
|---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | * General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
|---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <errno.h> |
|---|
| 24 | #include <types.h> |
|---|
| 25 | #include <event.h> |
|---|
| 26 | #include <device.h> |
|---|
| 27 | #include <driver.h> |
|---|
| 28 | #include <kmem.h> |
|---|
| 29 | #include <kdmsg.h> |
|---|
| 30 | #include <dma.h> |
|---|
| 31 | #include <kcm.h> |
|---|
| 32 | #include <thread.h> |
|---|
| 33 | #include <cluster.h> |
|---|
| 34 | #include <kcm.h> |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | KMEM_OBJATTR_INIT(dma_kmem_request_init) |
|---|
| 38 | { |
|---|
| 39 | attr->type = KMEM_DMA_REQUEST; |
|---|
| 40 | attr->name = "KCM Dev-Request"; |
|---|
| 41 | attr->size = sizeof(dev_request_t); |
|---|
| 42 | attr->aligne = 0; |
|---|
| 43 | attr->min = CONFIG_DMA_RQ_KCM_MIN; |
|---|
| 44 | attr->max = CONFIG_DMA_RQ_KCM_MAX; |
|---|
| 45 | attr->ctor = NULL; |
|---|
| 46 | attr->dtor = NULL; |
|---|
| 47 | |
|---|
| 48 | return 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | static EVENT_HANDLER(dma_async_request_event) |
|---|
| 52 | { |
|---|
| 53 | kmem_req_t req; |
|---|
| 54 | error_t err; |
|---|
| 55 | |
|---|
| 56 | if((err=event_get_error(event)) != 0) |
|---|
| 57 | printk(ERROR, "ERROR: dma_async_request_event: DMA transfare is not well completed, remaining %d\n", err); |
|---|
| 58 | |
|---|
| 59 | req.type = KMEM_DMA_REQUEST; |
|---|
| 60 | req.ptr = event_get_argument(event); |
|---|
| 61 | |
|---|
| 62 | assert(req.ptr != NULL && "Corrupted argument, expected the address of request"); |
|---|
| 63 | |
|---|
| 64 | kmem_free(&req); |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | static inline error_t dma_do_async_request(void *dst, void *src, size_t size) |
|---|
| 69 | { |
|---|
| 70 | kmem_req_t req; |
|---|
| 71 | dev_request_t *rq; |
|---|
| 72 | |
|---|
| 73 | req.type = KMEM_DMA_REQUEST; |
|---|
| 74 | req.size = sizeof(*rq); |
|---|
| 75 | req.flags = AF_KERNEL; |
|---|
| 76 | |
|---|
| 77 | if((rq = kmem_alloc(&req)) == NULL) |
|---|
| 78 | return ENOMEM; |
|---|
| 79 | |
|---|
| 80 | memset(rq, 0, sizeof(*rq)); |
|---|
| 81 | |
|---|
| 82 | rq->src = src; |
|---|
| 83 | rq->dst = dst; |
|---|
| 84 | rq->count = size; |
|---|
| 85 | rq->flags = DEV_RQ_NOBLOCK; |
|---|
| 86 | |
|---|
| 87 | event_set_priority(&rq->event, E_FUNC); |
|---|
| 88 | event_set_handler(&rq->event, &dma_async_request_event); |
|---|
| 89 | event_set_argument(&rq->event, rq); |
|---|
| 90 | |
|---|
| 91 | return __sys_dma->op.dev.write(__sys_dma, rq); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | static inline error_t dma_do_sync_request(void *dst, void *src, size_t size) |
|---|
| 95 | { |
|---|
| 96 | dev_request_t rq; |
|---|
| 97 | |
|---|
| 98 | rq.src = src; |
|---|
| 99 | rq.dst = dst; |
|---|
| 100 | rq.count = size; |
|---|
| 101 | rq.flags = 0; |
|---|
| 102 | |
|---|
| 103 | return __sys_dma->op.dev.write(__sys_dma, &rq); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | error_t dma_memcpy(void *dst, void *src, size_t size, uint_t isAsync) |
|---|
| 108 | { |
|---|
| 109 | if(isAsync == DMA_ASYNC) |
|---|
| 110 | return dma_do_async_request(dst, src, size); |
|---|
| 111 | else |
|---|
| 112 | return dma_do_sync_request(dst, src, size); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | int sys_dma_memcpy(void *src, void *dst, size_t size) |
|---|
| 117 | { |
|---|
| 118 | return dma_do_sync_request(dst, src, size); |
|---|
| 119 | } |
|---|