| 1 |
/* |
| 2 |
mtrr.c - Stuff for optimizing memory access |
| 3 |
Copyrights: |
| 4 |
2002 - Linux version by Nick Kurshev |
| 5 |
Licence: GPL |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "config.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <fcntl.h> |
| 15 |
#include <sys/ioctl.h> |
| 16 |
#include "kernelhelper/dhahelper.h" |
| 17 |
#include "libdha.h" |
| 18 |
|
| 19 |
#if defined (__i386__) && defined (__NetBSD__) |
| 20 |
#include <sys/param.h> |
| 21 |
#if __NetBSD_Version__ > 105240000 |
| 22 |
#include <stdint.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <machine/mtrr.h> |
| 25 |
#include <machine/sysarch.h> |
| 26 |
#endif |
| 27 |
#endif |
| 28 |
|
| 29 |
int mtrr_set_type(unsigned base,unsigned size,int type) |
| 30 |
{ |
| 31 |
int dhahelper_fd; |
| 32 |
dhahelper_fd = open("/dev/dhahelper",O_RDWR); |
| 33 |
if(dhahelper_fd > 0) |
| 34 |
{ |
| 35 |
int retval; |
| 36 |
dhahelper_mtrr_t mtrrs; |
| 37 |
mtrrs.operation = MTRR_OP_ADD; |
| 38 |
mtrrs.start = base; |
| 39 |
mtrrs.size = size; |
| 40 |
mtrrs.type = type; |
| 41 |
retval = ioctl(dhahelper_fd, DHAHELPER_ACK_IRQ, &mtrrs); |
| 42 |
close(dhahelper_fd); |
| 43 |
return retval; |
| 44 |
} |
| 45 |
#if defined (__NetBSD__) && (__NetBSD_Version__) > 105240000 |
| 46 |
{ |
| 47 |
struct mtrr *mtrrp; |
| 48 |
int n; |
| 49 |
|
| 50 |
mtrrp = malloc(sizeof (struct mtrr)); |
| 51 |
mtrrp->base = base; |
| 52 |
mtrrp->len = size; |
| 53 |
mtrrp->type = type; |
| 54 |
mtrrp->flags = MTRR_VALID | MTRR_PRIVATE; |
| 55 |
n = 1; |
| 56 |
|
| 57 |
if (i386_set_mtrr(mtrrp, &n) < 0) { |
| 58 |
free(mtrrp); |
| 59 |
return errno; |
| 60 |
} |
| 61 |
free(mtrrp); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
#else |
| 65 |
{ |
| 66 |
FILE * mtrr_fd; |
| 67 |
char * stype; |
| 68 |
switch(type) |
| 69 |
{ |
| 70 |
case MTRR_TYPE_UNCACHABLE: stype = "uncachable"; break; |
| 71 |
case MTRR_TYPE_WRCOMB: stype = "write-combining"; break; |
| 72 |
case MTRR_TYPE_WRTHROUGH: stype = "write-through"; break; |
| 73 |
case MTRR_TYPE_WRPROT: stype = "write-protect"; break; |
| 74 |
case MTRR_TYPE_WRBACK: stype = "write-back"; break; |
| 75 |
default: return EINVAL; |
| 76 |
} |
| 77 |
mtrr_fd = fopen("/proc/mtrr","wt"); |
| 78 |
if(mtrr_fd) |
| 79 |
{ |
| 80 |
char sout[256]; |
| 81 |
unsigned wr_len; |
| 82 |
sprintf(sout,"base=0x%08X size=0x%08X type=%s\n",base,size,stype); |
| 83 |
wr_len = fprintf(mtrr_fd,sout); |
| 84 |
/*printf("MTRR: %s\n",sout);*/ |
| 85 |
fclose(mtrr_fd); |
| 86 |
return wr_len == strlen(sout) ? 0 : EPERM; |
| 87 |
} |
| 88 |
} |
| 89 |
#endif |
| 90 |
return ENOSYS; |
| 91 |
} |