sharedregion.c 19.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  sharedregion.c
 *
 *  The SharedRegion module is designed to be used in a
 *  multi-processor environment where there are memory regions
 *  that are shared and accessed across different processors
 *
 *  Copyright (C) 2008-2009 Texas Instruments, Inc.
 *
 *  This package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE.
 */
19
#include <linux/module.h>
20 21 22
#include <linux/types.h>
#include <linux/string.h>
#include <linux/slab.h>
23
#include <syslink/atomic_linux.h>
24 25 26 27 28 29

#include <gt.h>
#include <multiproc.h>
#include <nameserver.h>
#include <sharedregion.h>

30 31 32
/* Macro to make a correct module magic number with refCount */
#define SHAREDREGION_MAKE_MAGICSTAMP(x)   ((SHAREDREGION_MODULEID << 16u) | (x))

33
#define SHAREDREGION_MAX_REGIONS_DEFAULT  4
34 35 36 37

/*
 *  Module state object
 */
38 39
struct sharedregion_module_object {
	atomic_t ref_count;   /* Reference count */
40
	struct mutex *gate_handle;
41 42 43 44 45 46 47 48 49
	struct sharedregion_info *table; /* Ptr to the table */
	u32 bitOffset;  /* Index bit offset */
	u32 region_size; /* Max size of each region */
	struct sharedregion_config cfg;	/* Current config values */
};

/*
 *  Shared region state object variable with default settings
 */
50
static struct sharedregion_module_object sharedregion_state = {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	.cfg.heap_handle = NULL,
	.cfg.gate_handle = NULL,
	.cfg.max_regions = SHAREDREGION_MAX_REGIONS_DEFAULT
};

/*
 * ======== sharedregion_get_config ========
 *  Purpose:
 *  This will get  sharedregion module configiguration
 */
int sharedregion_get_config(struct sharedregion_config *config)
{
	BUG_ON((config == NULL));
	memcpy(config, &sharedregion_state.cfg,
				sizeof(struct sharedregion_config));
	return 0;
}
68
EXPORT_SYMBOL(sharedregion_get_config);
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


/*
 * ======== sharedregion_get_bitoffset  ========
 *  Purpose:
 *  This will get get the bit offset
 */
static u32 sharedregion_get_bitoffset(u32 max_regions)
{
	u32 i;
	u32 bitoffset = 0;
	for (i = ((sizeof(void *) * 8) - 1); i >= 0; i--) {
		if (max_regions > (1 << i))
			break;
	}

	bitoffset = (((sizeof(void *) * 8) - 1) - i);
	return bitoffset;
}

/*
 * ======== sharedregion_setup ========
 *  Purpose:
 *  This will get setup the sharedregion module
 */
int sharedregion_setup(const struct sharedregion_config *config)
{
	struct sharedregion_config *tmpcfg = &sharedregion_state.cfg;
	struct sharedregion_info *table = NULL;
	u32 i;
	u32 j;
	s32 retval = 0;
	u16 proc_count;

103 104 105 106 107 108 109 110 111
	/* This sets the refCount variable is not initialized, upper 16 bits is
	* written with module Id to ensure correctness of refCount variable
	*/
	atomic_cmpmask_and_set(&sharedregion_state.ref_count,
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(0));

	if (atomic_inc_return(&sharedregion_state.ref_count)
				!= SHAREDREGION_MAKE_MAGICSTAMP(1)) {
112
		return 1;
113 114
	}

115 116 117 118 119 120
	if (config != NULL) {
		if (WARN_ON(config->max_regions == 0)) {
			retval = -EINVAL;
			goto error;
		}
		memcpy(&sharedregion_state.cfg, config,
121
				sizeof(struct sharedregion_config));
122 123 124
	}

	sharedregion_state.gate_handle = kmalloc(sizeof(struct mutex),
125
							GFP_KERNEL);
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
	if (sharedregion_state.gate_handle == NULL)
		goto gate_create_fail;

	sharedregion_state.bitOffset =
			sharedregion_get_bitoffset(tmpcfg->max_regions);
	sharedregion_state.region_size = (1 << sharedregion_state.bitOffset);
	proc_count = multiproc_get_max_processors();
	 /* TODO check heap usage & + 1 ? */
	sharedregion_state.table = kmalloc(sizeof(struct sharedregion_info) *
					tmpcfg->max_regions * (proc_count + 1),
					GFP_KERNEL);
	if (sharedregion_state.table == NULL) {
		retval = -ENOMEM;
		goto table_alloc_fail;
	}

	table = sharedregion_state.table;
	for (i = 0; i < tmpcfg->max_regions; i++) {
		for (j = 0; j < (proc_count + 1); j++) {
			(table + (j * tmpcfg->max_regions) + i)->is_valid =
									false;
			(table + (j * tmpcfg->max_regions) + i)->base = 0;
			(table + (j * tmpcfg->max_regions) + i)->len = 0;
		}
	}

	mutex_init(sharedregion_state.gate_handle);
	return 0;

table_alloc_fail:
	kfree(sharedregion_state.gate_handle);

gate_create_fail:
159 160
	memset(&sharedregion_state, 0,
		sizeof(struct sharedregion_module_object));
161 162 163
	sharedregion_state.cfg.max_regions = SHAREDREGION_MAX_REGIONS_DEFAULT;

error:
164
	printk(KERN_ERR "sharedregion_setup failed status:%x\n", retval);
165 166
	return retval;
}
167
EXPORT_SYMBOL(sharedregion_setup);
168 169 170 171 172 173 174 175 176 177 178

/*
 * ======== sharedregion_destroy ========
 *  Purpose:
 *  This will get destroy the sharedregion module
 */
int sharedregion_destroy(void)
{
	s32 retval = 0;
	void *gate_handle = NULL;

179 180 181
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
182 183 184 185
		retval = -ENODEV;
		goto error;
	}

186 187
	if (!(atomic_dec_return(&sharedregion_state.ref_count)
					== SHAREDREGION_MAKE_MAGICSTAMP(0))) {
188
		retval = 1; /* Syslink is not handling this on 2.0.0.06 */
189 190 191
		goto error;
	}

192 193 194 195 196 197
	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (retval)
		goto error;

	kfree(sharedregion_state.table);
	gate_handle = sharedregion_state.gate_handle; /* backup gate handle */
198 199
	memset(&sharedregion_state, 0,
		sizeof(struct sharedregion_module_object));
200 201 202 203 204 205
	sharedregion_state.cfg.max_regions = SHAREDREGION_MAX_REGIONS_DEFAULT;
	mutex_unlock(gate_handle);
	kfree(gate_handle);
	return 0;

error:
206
	printk(KERN_ERR "sharedregion_destroy failed status:%x\n", retval);
207 208
	return retval;
}
209
EXPORT_SYMBOL(sharedregion_destroy);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

/*
 * ======== sharedregion_add ========
 *  Purpose:
 *  This will add a memory segment to the lookup table
 *  during runtime by base and length
 */
int sharedregion_add(u32 index, void *base, u32 len)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	s32 retval = 0;
	u32 i;
	u16 myproc_id;
	bool overlap = false;
225
	bool same = false;
226

227 228 229
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		retval = -ENODEV;
		goto error;
	}

	if (index >= sharedregion_state.cfg.max_regions ||
			sharedregion_state.region_size < len) {
		retval = -EINVAL;
		goto error;
	}

	myproc_id = multiproc_get_id(NULL);
	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (retval)
		goto error;


	table = sharedregion_state.table;
	for (i = 0; i < sharedregion_state.cfg.max_regions; i++) {
		entry = (table
			+ (myproc_id * sharedregion_state.cfg.max_regions)
			+ i);
		if (entry->is_valid) {
252 253 254 255 256 257
			/* Handle duplicate entry */
			if((base == entry->base) && (len == entry->len)) {
				same = true;
				break;
			}

258 259 260 261 262 263
			if ((base >= entry->base) &&
			(base < (void *)((u32)entry->base + entry->len))) {
				overlap = true;
				break;
			}

264 265
			if ((base < entry->base) &&
			(void *)((u32)base + len) >= entry->base) {
266 267 268 269 270 271
				overlap = true;
				break;
			}
		}
	}

272 273 274 275 276
	if (same) {
		retval = 1;
		goto success;
	}

277
	if (overlap) {
278 279
		/* FHACK: FIX ME */
		retval =1;
280 281 282 283 284 285 286 287 288 289 290 291
		goto mem_overlap_error;
	}

	entry = (table
		 + (myproc_id * sharedregion_state.cfg.max_regions)
		 + index);
	if (entry->is_valid == false) {
		entry->base = base;
		entry->len = len;
		entry->is_valid = true;

	} else {
292 293
		/* FHACK: FIX ME */
		retval = 1;
294 295 296
		goto dup_entry_error;
	}

297
success:
298 299 300 301 302 303 304 305
	mutex_unlock(sharedregion_state.gate_handle);
	return 0;

dup_entry_error: /* Fall through */
mem_overlap_error:
	mutex_unlock(sharedregion_state.gate_handle);

error:
306
	printk(KERN_ERR "sharedregion_add failed status:%x\n", retval);
307 308
	return retval;
}
309
EXPORT_SYMBOL(sharedregion_add);
310 311 312 313 314 315 316 317 318 319 320 321 322

/*
 * ======== sharedregion_remove ========
 *  Purpose:
 *  This will removes a memory segment to the lookup table
 *  during runtime by base and length
 */
int sharedregion_remove(u32 index)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	s32 retval = 0;

323 324 325
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
		retval = -ENODEV;
		goto error;
	}

	if (index >= sharedregion_state.cfg.max_regions) {
		retval = -EINVAL;
		goto error;
	}

	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (retval)
		goto error;

	table = sharedregion_state.table;
	entry = (table + index);
	entry->is_valid = false;
	entry->base = NULL;
	entry->len = 0;
	mutex_unlock(sharedregion_state.gate_handle);
	return 0;

error:
348
	printk(KERN_ERR "sharedregion_remove failed status:%x\n", retval);
349 350
	return retval;
}
351
EXPORT_SYMBOL(sharedregion_remove);
352 353 354 355 356 357 358 359 360 361 362 363 364

/*
 * ======== sharedregion_get_index ========
 *  Purpose:
 *  This will return the index for the specified address pointer.
 */
int sharedregion_get_index(void *addr)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	bool found = false;
	u32 i;
	u16 myproc_id;
365
	s32 retval = 0;
366

367 368 369
	if (WARN_ON(atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true)) {
370 371 372 373 374 375
		retval = -ENODEV;
		goto exit;
	}

	myproc_id = multiproc_get_id(NULL);
	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
376 377
	if (retval) {
		retval = -ENODEV;
378
		goto exit;
379
	}
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

	table = sharedregion_state.table;
	for (i = 0; i < sharedregion_state.cfg.max_regions; i++) {
		entry = (table
			+ (myproc_id * sharedregion_state.cfg.max_regions)
			+ i);
		if ((addr >= entry->base) &&
		(addr < (void *)((u32)entry->base + (entry->len)))) {
			found = true;
			break;
		}
	}

	if (found)
		retval = i;
	else
		retval = -ENOENT; /* No entry found in the table */

	mutex_unlock(sharedregion_state.gate_handle);
399
	return retval;
400 401

exit:
402
	printk(KERN_ERR "sharedregion_get_index failed index:%x\n", retval);
403 404
	return retval;
}
405
EXPORT_SYMBOL(sharedregion_get_index);
406 407 408 409 410 411 412 413 414 415 416 417 418 419

/*
 * ======== sharedregion_get_ptr ========
 *  Purpose:
 *  This will return the address pointer associated with the
 *  shared region pointer
 */
void *sharedregion_get_ptr(u32 *srptr)
{
	struct sharedregion_info *entry = NULL;
	void *ptr = NULL;
	u16 myproc_id;
	s32 retval = 0;

420 421 422 423
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
424
		goto error;
425
	}
426

427
	if (srptr == SHAREDREGION_INVALIDSRPTR)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		goto error;

	myproc_id = multiproc_get_id(NULL);
	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (WARN_ON(retval != 0))
		goto error;

	entry = (sharedregion_state.table
		 + (myproc_id * sharedregion_state.cfg.max_regions)
		 + ((u32)srptr >> sharedregion_state.bitOffset));
	/* TO DO check:: is this correct ? */
	ptr = ((void *)(((u32)srptr &
		((1 << sharedregion_state.bitOffset) - 1)) + (u32)entry->base));
	mutex_unlock(sharedregion_state.gate_handle);
	return ptr;

error:
445
	printk(KERN_ERR "sharedregion_get_ptr failed \n");
446 447 448
	return (void *)NULL;

}
449
EXPORT_SYMBOL(sharedregion_get_ptr);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

/*
 * ======== sharedregion_get_srptr ========
 *  Purpose:
 *  This will return sharedregion pointer associated with the
 *  an address in a shared region area registered with the
 *  sharedregion module
 */
u32 *sharedregion_get_srptr(void *addr, s32 index)
{
	struct sharedregion_info *entry = NULL;
	u32 *ptr = SHAREDREGION_INVALIDSRPTR ;
	u32 myproc_id;
	s32 retval = 0;

465 466 467 468
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
469
		goto error;
470
	}
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

	if (WARN_ON(addr == NULL))
		goto error;

	if (WARN_ON(index >= sharedregion_state.cfg.max_regions))
		goto error;

	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (WARN_ON(retval != 0))
		goto error;

	myproc_id = multiproc_get_id(NULL);
	entry = (sharedregion_state.table
		+ (myproc_id * sharedregion_state.cfg.max_regions)
		+ index);
	ptr = (u32 *) ((index << sharedregion_state.bitOffset)
				| ((u32)addr - (u32)entry->base));
	mutex_unlock(sharedregion_state.gate_handle);
	return ptr;

error:
492
	printk(KERN_ERR	"sharedregion_get_srptr failed\n");
493 494
	return (u32 *)NULL;
}
495
EXPORT_SYMBOL(sharedregion_get_srptr);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

/*
 * ======== sharedregion_get_table_info ========
 *  Purpose:
 *  This will get the table entry information for the
 *  specified index and id
 */
int sharedregion_get_table_info(u32 index, u16 proc_id,
				struct sharedregion_info *info)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	u16 proc_count;
	s32 retval = 0;

511
	BUG_ON(info == NULL);
512 513 514
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		retval = -ENODEV;
		goto error;
	}

	proc_count = multiproc_get_max_processors();
	if (index >= sharedregion_state.cfg.max_regions ||
						proc_id >= proc_count) {
		retval = -EINVAL;
		goto error;
	}

	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (retval)
		goto error;

	table = sharedregion_state.table;
	entry = (table
		+ (proc_id * sharedregion_state.cfg.max_regions)
		+ index);
	memcpy((void *) info, (void *) entry, sizeof(struct sharedregion_info));
	mutex_unlock(sharedregion_state.gate_handle);
	return 0;

error:
539 540
	printk(KERN_ERR "sharedregion_get_table_info failed status:%x\n",
			retval);
541 542
	return retval;
}
543
EXPORT_SYMBOL(sharedregion_get_table_info);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

/*
 * ======== sharedregion_set_table_info ========
 *  Purpose:
 *  This will set the table entry information for the
 *  specified index and id
 */
int sharedregion_set_table_info(u32 index, u16 proc_id,
				struct sharedregion_info *info)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	u16 proc_count;
	s32 retval = 0;

	BUG_ON(info != NULL);
560 561 562
	if (atomic_cmpmask_and_lt(&(sharedregion_state.ref_count),
				SHAREDREGION_MAKE_MAGICSTAMP(0),
				SHAREDREGION_MAKE_MAGICSTAMP(1)) == true) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		retval = -ENODEV;
		goto error;
	}

	proc_count = multiproc_get_max_processors();
	if (index >= sharedregion_state.cfg.max_regions ||
						proc_id >= proc_count) {
		retval = -EINVAL;
		goto error;
	}

	retval = mutex_lock_interruptible(sharedregion_state.gate_handle);
	if (retval)
		goto error;

	table = sharedregion_state.table;
	entry = (table
		+ (proc_id * sharedregion_state.cfg.max_regions)
		+ index);
	memcpy((void *) entry, (void *) info, sizeof(struct sharedregion_info));
	mutex_unlock(sharedregion_state.gate_handle);
	return 0;

error:
587 588
	printk(KERN_ERR "sharedregion_set_table_info failed status:%x\n",
			retval);
589 590
	return retval;
}
591
EXPORT_SYMBOL(sharedregion_set_table_info);
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

/*
 * ======== Sharedregion_attach ========
 *  Purpose:
 *  This will attachs the shared region with an proc_id
 *
 *  Application should call this function from the callback
 *  function registered for device attach to the processor
 *  manager. All modules which requires some logic setup
 *  to be done when a device gets attach to the system,
 *  should export API like this. Please see the below psuedo
 *  code for example:
 *  Example
 *  code
 *      void function (proc_id, config) {
 *          NotifyDriver_attach (proc_id, config->ndParams);
 *           SysMemMgr_attach (proc_id);
 *           SMM_attach (proc_id);
 *            NameServerRemoteTransport_attach(proc_id, config->nsrtParams);
 *            SharedRegion_attach (proc_id);
 *            SharedMemory_getConfig (&cfg);
 *             for (i = 0u; i < cfg->maxRegions; i++) {
 *                  SharedRegion_getTableInfo(i, &myinfo,   myProcId);
 *                  SharedRegion_getTableInfo(i, &peerinfo, proc_id);
 *                   DMM_map (proc_id,
 *                           PA(myinfo->vaddr),
 *                            peerinfo->vaddr,
 *                            myinfo->len);
 *               }
 *               ...
 *          }
 *
 *          main () {
 *               # attach callback for device attach only
 *                ProcMgr_register (function, proc_id, DEV_ATTACH);
 *          }
 *
 */
void sharedregion_attach(u16 proc_id)
{
	struct sharedregion_info *entry = NULL;
	struct sharedregion_info *table = NULL;
	char *hexstr = "0123456789ABCDEF";
	char tname[80];
	u16 proc_id_list[2];
	u32 addr = 0;
	u32 len;
	u16 proc_count;
	void *nshandle;
	s32 retval = 0;
	s32 i;

	if (WARN_ON(sharedregion_state.table == NULL))
		goto error;

	proc_count = multiproc_get_max_processors();
	if (WARN_ON(proc_id >= proc_count))
		goto error;

	proc_id_list[0] = proc_id;
	proc_id_list[1] = MULTIPROC_INVALIDID;
	nshandle = nameserver_get_handle(SHAREDREGION_NAMESERVER);
	if (nshandle == NULL)
		goto error;

	/* Get Shared region entries from the remote shared region nameserver */
	for (i = 0u; i < sharedregion_state.cfg.max_regions; i++) {
		memset(tname, 0, 80);
		strcpy(tname, "SHAREDREGION:SRENTRY_ADDR_");
		tname[strlen(tname)] = hexstr[(proc_id >> 4u) & 0xF];
		tname[strlen(tname)] = hexstr[proc_id & 0xF];
		tname[strlen(tname)] = '_';
		tname[strlen(tname)] = hexstr[(i >> 28u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 24u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 20u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 16u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 12u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >>  8u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >>  4u) & 0xF];
		tname[strlen(tname)] = hexstr[i & 0xF];
		retval = nameserver_get(nshandle, tname,
					&len, sizeof(u32), &proc_id_list[0]);
		if (WARN_ON(retval))
			;

		memset(tname, 0, 80u);
		strcpy(tname, "SHAREDREGION:SRENTRY_LEN_");
		tname[strlen(tname)] = hexstr[(proc_id >> 4u) & 0xF];
		tname[strlen(tname)] = hexstr[proc_id & 0xF];
		tname[strlen(tname)] = '_';
		tname[strlen(tname)] = hexstr[(i >> 28u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 24u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 20u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 16u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >> 12u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >>  8u) & 0xF];
		tname[strlen(tname)] = hexstr[(i >>  4u) & 0xF];
		tname[strlen(tname)] = hexstr[i & 0xF];

		/* TO DO : check this */
		retval = nameserver_get(nshandle, tname,
					&len, sizeof(u32), &proc_id_list[0]);
		if (WARN_ON(retval))
			;

		/* Found an entry in the remote nameserver */
		/* Add it into the shared region table */
		if (retval == 0) {
			retval = mutex_lock_interruptible(
					sharedregion_state.gate_handle);
			if (WARN_ON(retval))
				break;

			table = sharedregion_state.table;
			/* mark entry invalid */
			entry = (table
				+ (proc_id * sharedregion_state.cfg.max_regions)
				+ i);
			entry->base    = (void *)addr;
			entry->len     = len;
			entry->is_valid = false;
			mutex_unlock(sharedregion_state.gate_handle);
		}

	}

error:
	return;
}
721
EXPORT_SYMBOL(sharedregion_attach);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

/*
 * ======== Sharedregion_detach ========
 *  Purpose:
 *  This will detachs the shared region for an proc_id
 *
 *  Application should call this function from the callback
 *  function registered for device detach to the processorl
 *  manager. All modules which requires some logic setup
 *  to be done when a device gets detach from the system,
 *  should export API like this.
 *  Please see the below psuedo code for example:
 *  @Example
 *  @code
 *    void function (proc_id) {
 *    	SharedRegion_detach (proc_id);
 *    	SysMemMgr_detach (proc_id);
 *     	...
 *   	# Name server must be detached last
 *    	nameserver_detach (proc_id);
 *     }
 *
 *    main () {
 *    	# attach callback for device detach only
 *	Processor_register (function, proc_id, DEV_DETACH);
 *    }
 *
 */
void sharedregion_detach(u16 proc_id)
{
	u16 proc_count;

	if (WARN_ON(sharedregion_state.table == NULL))
		goto error;

	proc_count = multiproc_get_max_processors();
	if (WARN_ON(proc_id >= proc_count))
		goto error;

error:
	return;
}
764
EXPORT_SYMBOL(sharedregion_detach);
765