blob: f4ecac0e50929828d1b364e1cba025e0644e86af [file] [log] [blame]
Ken Cox9d9baad2014-03-04 07:58:05 -06001/* memregion_direct.c
2 *
Benjamin Romerf6d0c1e2014-04-23 14:58:34 -04003 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
Ken Cox9d9baad2014-03-04 07:58:05 -06004 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 */
17
18/*
19 * This is an implementation of memory regions that can be used to read/write
20 * channel memory (in main memory of the host system) from code running in
21 * a virtual partition.
22 */
23#include "uniklog.h"
24#include "timskmod.h"
25#include "memregion.h"
26
27#define MYDRVNAME "memregion"
28
Benjamin Romerb63438c2014-11-04 11:25:15 -050029struct memregion {
Ken Cox9d9baad2014-03-04 07:58:05 -060030 HOSTADDRESS physaddr;
31 ulong nbytes;
Ken Cox3db55402014-05-22 12:31:09 -050032 void __iomem *mapped;
Ken Cox9d9baad2014-03-04 07:58:05 -060033 BOOL requested;
34 BOOL overlapped;
35};
36
Benjamin Romerb63438c2014-11-04 11:25:15 -050037static BOOL mapit(struct memregion *memregion);
38static void unmapit(struct memregion *memregion);
Ken Cox9d9baad2014-03-04 07:58:05 -060039
Benjamin Romerb63438c2014-11-04 11:25:15 -050040struct memregion *
Ken Cox927c7922014-03-05 14:52:25 -060041visor_memregion_create(HOSTADDRESS physaddr, ulong nbytes)
Ken Cox9d9baad2014-03-04 07:58:05 -060042{
Benjamin Romerb63438c2014-11-04 11:25:15 -050043 struct memregion *rc = NULL;
Quentin Lambertea0dcfc2015-02-10 15:12:07 +010044 struct memregion *memregion;
45
46 memregion = kzalloc(sizeof(*memregion), GFP_KERNEL | __GFP_NORETRY);
Ken Cox9d9baad2014-03-04 07:58:05 -060047 if (memregion == NULL) {
Ken Cox927c7922014-03-05 14:52:25 -060048 ERRDRV("visor_memregion_create allocation failed");
Ken Cox9d9baad2014-03-04 07:58:05 -060049 return NULL;
50 }
Ken Cox9d9baad2014-03-04 07:58:05 -060051 memregion->physaddr = physaddr;
52 memregion->nbytes = nbytes;
53 memregion->overlapped = FALSE;
Ken Coxd9355f82014-03-19 13:06:22 -050054 if (!mapit(memregion)) {
55 rc = NULL;
Benjamin Romerfef48382014-11-04 11:25:16 -050056 goto cleanup;
Ken Coxd9355f82014-03-19 13:06:22 -050057 }
58 rc = memregion;
Benjamin Romerfef48382014-11-04 11:25:16 -050059cleanup:
Ken Cox9d9baad2014-03-04 07:58:05 -060060 if (rc == NULL) {
Sudip Mukherjeef9b64692014-11-07 17:48:34 +053061 visor_memregion_destroy(memregion);
62 memregion = NULL;
Ken Cox9d9baad2014-03-04 07:58:05 -060063 }
64 return rc;
65}
Ken Cox927c7922014-03-05 14:52:25 -060066EXPORT_SYMBOL_GPL(visor_memregion_create);
Ken Cox9d9baad2014-03-04 07:58:05 -060067
Benjamin Romerb63438c2014-11-04 11:25:15 -050068struct memregion *
69visor_memregion_create_overlapped(struct memregion *parent, ulong offset,
70 ulong nbytes)
Ken Cox9d9baad2014-03-04 07:58:05 -060071{
Benjamin Romerb63438c2014-11-04 11:25:15 -050072 struct memregion *memregion = NULL;
Ken Cox9d9baad2014-03-04 07:58:05 -060073
74 if (parent == NULL) {
75 ERRDRV("%s parent is NULL", __func__);
76 return NULL;
77 }
78 if (parent->mapped == NULL) {
79 ERRDRV("%s parent is not mapped!", __func__);
80 return NULL;
81 }
82 if ((offset >= parent->nbytes) ||
83 ((offset + nbytes) >= parent->nbytes)) {
84 ERRDRV("%s range (%lu,%lu) out of parent range",
85 __func__, offset, nbytes);
86 return NULL;
87 }
Benjamin Romer6db05d32014-11-04 11:25:20 -050088 memregion = kzalloc(sizeof(*memregion), GFP_KERNEL|__GFP_NORETRY);
Ken Cox9d9baad2014-03-04 07:58:05 -060089 if (memregion == NULL) {
90 ERRDRV("%s allocation failed", __func__);
91 return NULL;
92 }
Andreea-Cristina Bernat97a84f12014-03-14 04:20:06 +020093
Ken Cox9d9baad2014-03-04 07:58:05 -060094 memregion->physaddr = parent->physaddr + offset;
95 memregion->nbytes = nbytes;
Benjamin Romer49695a532014-11-04 11:25:17 -050096 memregion->mapped = ((u8 __iomem *)(parent->mapped)) + offset;
Ken Cox9d9baad2014-03-04 07:58:05 -060097 memregion->requested = FALSE;
98 memregion->overlapped = TRUE;
99 return memregion;
100}
Ken Cox927c7922014-03-05 14:52:25 -0600101EXPORT_SYMBOL_GPL(visor_memregion_create_overlapped);
Ken Cox9d9baad2014-03-04 07:58:05 -0600102
Ken Cox9d9baad2014-03-04 07:58:05 -0600103static BOOL
Benjamin Romerb63438c2014-11-04 11:25:15 -0500104mapit(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600105{
Benjamin Romer49695a532014-11-04 11:25:17 -0500106 ulong physaddr = (ulong)(memregion->physaddr);
Ken Cox9d9baad2014-03-04 07:58:05 -0600107 ulong nbytes = memregion->nbytes;
108
109 memregion->requested = FALSE;
110 if (!request_mem_region(physaddr, nbytes, MYDRVNAME))
Benjamin Romer92d00cf2014-11-04 11:25:19 -0500111 ERRDRV("cannot reserve channel memory @0x%lx for 0x%lx-- no big deal",
112 physaddr, nbytes);
Ken Cox9d9baad2014-03-04 07:58:05 -0600113 else
114 memregion->requested = TRUE;
115 memregion->mapped = ioremap_cache(physaddr, nbytes);
116 if (memregion->mapped == NULL) {
117 ERRDRV("cannot ioremap_cache channel memory @0x%lx for 0x%lx",
118 physaddr, nbytes);
119 return FALSE;
120 }
121 return TRUE;
122}
123
124static void
Benjamin Romerb63438c2014-11-04 11:25:15 -0500125unmapit(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600126{
127 if (memregion->mapped != NULL) {
128 iounmap(memregion->mapped);
129 memregion->mapped = NULL;
130 }
131 if (memregion->requested) {
Benjamin Romer49695a532014-11-04 11:25:17 -0500132 release_mem_region((ulong)(memregion->physaddr),
Ken Cox9d9baad2014-03-04 07:58:05 -0600133 memregion->nbytes);
134 memregion->requested = FALSE;
135 }
136}
137
138HOSTADDRESS
Benjamin Romerb63438c2014-11-04 11:25:15 -0500139visor_memregion_get_physaddr(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600140{
141 return memregion->physaddr;
142}
Ken Cox927c7922014-03-05 14:52:25 -0600143EXPORT_SYMBOL_GPL(visor_memregion_get_physaddr);
Ken Cox9d9baad2014-03-04 07:58:05 -0600144
145ulong
Benjamin Romerb63438c2014-11-04 11:25:15 -0500146visor_memregion_get_nbytes(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600147{
148 return memregion->nbytes;
149}
Ken Cox927c7922014-03-05 14:52:25 -0600150EXPORT_SYMBOL_GPL(visor_memregion_get_nbytes);
Ken Cox9d9baad2014-03-04 07:58:05 -0600151
Ken Cox3db55402014-05-22 12:31:09 -0500152void __iomem *
Benjamin Romerb63438c2014-11-04 11:25:15 -0500153visor_memregion_get_pointer(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600154{
155 return memregion->mapped;
156}
Ken Cox927c7922014-03-05 14:52:25 -0600157EXPORT_SYMBOL_GPL(visor_memregion_get_pointer);
Ken Cox9d9baad2014-03-04 07:58:05 -0600158
159int
Benjamin Romerb63438c2014-11-04 11:25:15 -0500160visor_memregion_resize(struct memregion *memregion, ulong newsize)
Ken Cox9d9baad2014-03-04 07:58:05 -0600161{
162 if (newsize == memregion->nbytes)
163 return 0;
164 if (memregion->overlapped)
165 /* no error check here - we no longer know the
166 * parent's range!
167 */
168 memregion->nbytes = newsize;
169 else {
170 unmapit(memregion);
171 memregion->nbytes = newsize;
172 if (!mapit(memregion))
173 return -1;
174 }
175 return 0;
176}
Ken Cox927c7922014-03-05 14:52:25 -0600177EXPORT_SYMBOL_GPL(visor_memregion_resize);
Ken Cox9d9baad2014-03-04 07:58:05 -0600178
Ken Cox9d9baad2014-03-04 07:58:05 -0600179static int
180memregion_readwrite(BOOL is_write,
Benjamin Romerb63438c2014-11-04 11:25:15 -0500181 struct memregion *memregion, ulong offset,
Ken Cox9d9baad2014-03-04 07:58:05 -0600182 void *local, ulong nbytes)
183{
184 if (offset + nbytes > memregion->nbytes) {
185 ERRDRV("memregion_readwrite offset out of range!!");
Benjamin Romer66e24b72014-07-25 13:55:10 -0400186 return -EIO;
Ken Cox9d9baad2014-03-04 07:58:05 -0600187 }
188 if (is_write)
189 memcpy_toio(memregion->mapped + offset, local, nbytes);
190 else
191 memcpy_fromio(local, memregion->mapped + offset, nbytes);
192
193 return 0;
194}
195
196int
Benjamin Romerb63438c2014-11-04 11:25:15 -0500197visor_memregion_read(struct memregion *memregion, ulong offset, void *dest,
Ken Cox927c7922014-03-05 14:52:25 -0600198 ulong nbytes)
Ken Cox9d9baad2014-03-04 07:58:05 -0600199{
200 return memregion_readwrite(FALSE, memregion, offset, dest, nbytes);
201}
Ken Cox927c7922014-03-05 14:52:25 -0600202EXPORT_SYMBOL_GPL(visor_memregion_read);
Ken Cox9d9baad2014-03-04 07:58:05 -0600203
204int
Benjamin Romerb63438c2014-11-04 11:25:15 -0500205visor_memregion_write(struct memregion *memregion, ulong offset, void *src,
Ken Cox927c7922014-03-05 14:52:25 -0600206 ulong nbytes)
Ken Cox9d9baad2014-03-04 07:58:05 -0600207{
208 return memregion_readwrite(TRUE, memregion, offset, src, nbytes);
209}
Ken Cox927c7922014-03-05 14:52:25 -0600210EXPORT_SYMBOL_GPL(visor_memregion_write);
Ken Cox9d9baad2014-03-04 07:58:05 -0600211
212void
Benjamin Romerb63438c2014-11-04 11:25:15 -0500213visor_memregion_destroy(struct memregion *memregion)
Ken Cox9d9baad2014-03-04 07:58:05 -0600214{
215 if (memregion == NULL)
216 return;
217 if (!memregion->overlapped)
218 unmapit(memregion);
219 kfree(memregion);
220}
Ken Cox927c7922014-03-05 14:52:25 -0600221EXPORT_SYMBOL_GPL(visor_memregion_destroy);
Ken Cox9d9baad2014-03-04 07:58:05 -0600222